<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Models\Textbook;
use App\Models\Question;

class SourcePaper extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'source_file_id',
        'order',
        'order_index',
        'paper_code',
        'title',
        'full_title',
        'chapter',
        'subject',
        'grade',
        'term',
        'edition',
        'series_id',
        'textbook_id',
        'source_type',
        'source_year',
        'raw_markdown',
        'detected_metadata',
        'meta',
    ];

    protected $casts = [
        'detected_metadata' => 'array',
        'meta' => 'array',
        'order' => 'integer',
        'order_index' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

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

    public function parts(): HasMany
    {
        return $this->hasMany(PaperPart::class, 'source_paper_id');
    }

    public function textbook(): BelongsTo
    {
        return $this->belongsTo(Textbook::class, 'textbook_id');
    }

    public function questions(): HasMany
    {
        return $this->hasMany(Question::class, 'source_paper_id');
    }

    public function candidates(): HasMany
    {
        return $this->hasMany(PreQuestionCandidate::class, 'source_paper_id');
    }

    public function questionRefs(): HasMany
    {
        return $this->hasMany(PaperQuestionRef::class, 'source_paper_id');
    }

    public function series(): BelongsTo
    {
        return $this->belongsTo(TextbookSeries::class, 'series_id');
    }
}
