<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PaperQuestion extends Model
{
    protected $table = 'paper_questions';
    protected $primaryKey = 'id';
    
    protected $fillable = [
        'paper_id',
        'question_id',
        'question_bank_id',
        'knowledge_point',
        'question_type',  // choice-选择题, fill-填空题, answer-解答题
        'question_text',
        'correct_answer',
        'solution',  // 解题思路
        'difficulty',
        'score',
        'estimated_time',
        'question_number',
        'student_answer',
        'is_correct',
        'score_obtained',
        'score_ratio',
        'teacher_comment',
        'graded_at',
        'graded_by',
    ];
    
    protected $casts = [
        'paper_id' => 'string',
        'question_id' => 'string',
        'question_bank_id' => 'integer',
        'question_type' => 'string',
        'question_text' => 'string',
        'difficulty' => 'float',
        'score' => 'float',
        'estimated_time' => 'integer',
        'question_number' => 'integer',
        'is_correct' => 'boolean',
        'score_obtained' => 'float',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];
    
    /**
     * 获取所属试卷
     */
    public function paper()
    {
        return $this->belongsTo(Paper::class, 'paper_id', 'paper_id');
    }

    /**
     * 获取对应的题库题目
     */
    public function questionBank()
    {
        return $this->belongsTo(\App\Models\Question::class, 'question_bank_id', 'id');
    }

    /**
     * 按 question_id 查询题目的作用域
     */
    public function scopeByQuestionId($query, $questionId)
    {
        return $query->where('question_id', $questionId);
    }

    /**
     * 按 question_bank_id 查询题目的作用域
     */
    public function scopeByQuestionBankId($query, $questionBankId)
    {
        return $query->where('question_bank_id', $questionBankId);
    }
}
