<?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\Question;

class PaperPart extends Model
{
    use HasFactory;

    protected $fillable = [
        'source_paper_id',
        'order',
        'order_index',
        'title',
        'type',
        'part_label',
        'raw_markdown',
        'question_count',
        'detected_features',
        'meta',
    ];

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

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

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

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

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