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

/**
 * 考试作答记录
 */
class Attempt extends Model
{
    protected $table = 'attempts';

    protected $fillable = [
        'exam_id',
        'student_id',
        'score',
        'status',
        'switch_count',
        'started_at',
        'submitted_at',
    ];

    protected $casts = [
        'score' => 'decimal:1',
        'status' => AttemptStatus::class,
        'switch_count' => 'integer',
        'started_at' => 'datetime',
        'submitted_at' => 'datetime',
    ];

    /**
     * 关联考试
     */
    public function exam(): BelongsTo
    {
        return $this->belongsTo(Exam::class, 'exam_id');
    }

    /**
     * 关联学员
     */
    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class, 'student_id');
    }

    /**
     * 答题明细
     */
    public function answers(): HasMany
    {
        return $this->hasMany(AttemptAnswer::class, 'attempt_id');
    }
}
