<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Quiz extends Model
{
    protected $fillable = [
        'subject_id', 'teacher_id', 'title',
        'topic', 'max_score', 'quiz_date',
    ];

    protected $casts = ['quiz_date' => 'date'];

    public function subject()
    {
        return $this->belongsTo(Subject::class);
    }

    public function teacher()
    {
        return $this->belongsTo(User::class, 'teacher_id');
    }

    public function marks()
    {
        return $this->hasMany(Mark::class);
    }

    public function markForStudent(int $studentId): ?Mark
    {
        return $this->marks()->where('student_id', $studentId)->first();
    }
}
