<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\SourceFile;
use App\Models\SourcePaper;
use App\Models\PaperPart;

class PreQuestionCandidate extends Model
{
    use HasFactory;

    protected $fillable = [
        'import_id',
        'source_file_id',
        'source_paper_id',
        'part_id',
        'sequence',
        'index',
        'order_index',
        'question_number',
        'order',
        'raw_markdown',
        'raw_hash',
        'raw_text',
        'clean_markdown',
        'clean_hash',
        'structured_json',
        'stem',
        'options',
        'images',
        'tables',
        'is_question_candidate',
        'ai_confidence',
        'confidence',
        'formula_detected',
        'is_valid_question',
        'status',
        'meta',
    ];

    protected $casts = [
        'is_question_candidate' => 'boolean',
        'ai_confidence' => 'float',
        'confidence' => 'decimal:2',
        'sequence' => 'integer',
        'order' => 'integer',
        'options' => 'array',
        'images' => 'array',
        'tables' => 'array',
        'formula_detected' => 'boolean',
        'is_valid_question' => 'boolean',
        'meta' => 'array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public const STATUS_PENDING = 'pending';
    public const STATUS_REVIEWED = 'reviewed';
    public const STATUS_ACCEPTED = 'accepted';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_SUPERSEDED = 'superseded';

    public function import(): BelongsTo
    {
        return $this->belongsTo(MarkdownImport::class, 'import_id');
    }

    public function sourceFile(): BelongsTo
    {
        return $this->belongsTo(SourceFile::class, 'source_file_id');
    }

    public function sourcePaper(): BelongsTo
    {
        return $this->belongsTo(SourcePaper::class, 'source_paper_id');
    }

    public function part(): BelongsTo
    {
        return $this->belongsTo(PaperPart::class, 'part_id');
    }

    public function getConfidenceBadgeAttribute(): string
    {
        if ($this->ai_confidence === null) {
            return 'gray';
        }

        if ($this->ai_confidence >= 0.8) {
            return 'success';
        } elseif ($this->ai_confidence >= 0.5) {
            return 'warning';
        }

        return 'danger';
    }

    public function getStatusBadgeAttribute(): string
    {
        $badges = [
            self::STATUS_PENDING => 'gray',
            self::STATUS_REVIEWED => 'info',
            self::STATUS_ACCEPTED => 'success',
            self::STATUS_REJECTED => 'danger',
            self::STATUS_SUPERSEDED => 'gray',
        ];

        return $badges[$this->status] ?? 'gray';
    }

    public function getFirstImageAttribute(): ?string
    {
        if (empty($this->images)) {
            return null;
        }

        $images = is_string($this->images) ? json_decode($this->images, true) : $this->images;
        return $images[0] ?? null;
    }

    public function getStemPreviewAttribute(): string
    {
        if (!$this->stem) {
            return 'No stem extracted';
        }

        return \Str::limit($this->stem, 100);
    }
}
