<?php

namespace App\Models;

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

class SourceFile extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'original_filename',
        'normalized_filename',
        'extension',
        'storage_path',
        'raw_markdown',
        'file_metadata',
        'extracted_metadata',
        'source_type',
        'filename',
        'path',
        'checksum',
        'version',
        'meta',
        'status',
        'created_by',
    ];

    protected $casts = [
        'file_metadata' => 'array',
        'extracted_metadata' => 'array',
        'meta' => 'array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function papers(): HasMany
    {
        return $this->hasMany(SourcePaper::class);
    }

    public function parts(): HasManyThrough
    {
        return $this->hasManyThrough(PaperPart::class, SourcePaper::class);
    }

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

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