<?php

declare(strict_types=1);

namespace Kylin\Education\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Education\Enums\ExamStatus;
use Kylin\Education\Enums\ExamType;

/**
 * 考试模型
 *
 * 在线考试管理，支持限时考试、随机出题、自动评分。
 *
 * @property int    $id
 * @property string $title   考试标题
 * @property ExamType $type  考试类型
 * @property int    $duration 考试时长（分钟）
 */
class Exam extends Model
{
    protected $table = 'exams';

    protected $fillable = [
        'name',
        'course_id',
        'type',
        'duration',
        'pass_score',
        'total_score',
        'start_time',
        'end_time',
        'anti_cheat',
        'status',
    ];

    protected $casts = [
        'type' => ExamType::class,
        'status' => ExamStatus::class,
        'duration' => 'integer',
        'pass_score' => 'integer',
        'total_score' => 'integer',
        'start_time' => 'datetime',
        'end_time' => 'datetime',
        'anti_cheat' => 'array',
    ];

    /**
     * 关联课程
     */
    public function course(): BelongsTo
    {
        return $this->belongsTo(Course::class, 'course_id');
    }

    /**
     * 关联题目
     */
    public function questions(): HasMany
    {
        return $this->hasMany(ExamQuestion::class, 'exam_id');
    }

    /**
     * 关联答题记录
     */
    public function attempts(): HasMany
    {
        return $this->hasMany(Attempt::class, 'exam_id');
    }
}
