<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ExamQuestion extends Model
{
    protected $fillable = [
        'exam_attempt_id',
        'exam_id',
        'question_id',
        'course_id',
        'question_text',
        'explanation',
        'options_snapshot',
        'correct_option_id',
        'correct_option_key',
        'correct_option_text',
        'selected_option_id',
        'selected_option_key',
        'selected_option_text',
        'is_correct',
        'sort_order',
    ];

    protected $casts = [
        'options_snapshot' => 'array',
        'is_correct' => 'boolean',
    ];

    public function attempt()
    {
        return $this->belongsTo(ExamAttempt::class, 'exam_attempt_id');
    }

    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function question()
    {
        return $this->belongsTo(Question::class);
    }

    public function course()
    {
        return $this->belongsTo(Course::class);
    }

    public function scopeCorrect($query)
    {
        return $query->where('is_correct', true);
    }

    public function scopeWrong($query)
    {
        return $query->where('is_correct', false);
    }

    public function scopeUnanswered($query)
    {
        return $query->whereNull('selected_option_id');
    }
}