<?php

namespace App\Models;

use Carbon\Carbon;
use App\Models\ExamQuestion;
use Illuminate\Database\Eloquent\Model;

class Exam extends Model
{
    protected $fillable = [
        'name',
        'status',
        'lecture_id',
        'retake',
    ];
    public function lecture()
    {
        return $this->belongsTo(Lecture::class);
    }
    public function questions() // Get all questions for the exam
    {
        return $this->hasMany(ExamQuestion::class)->orderBy('order', 'asc');
    }
    public function randomQuestions() // Get random questions for the exam
    {
        return $this->hasMany(ExamQuestion::class)->inRandomOrder();
    }
    public function getCreatedAtAttribute($date)
    {
        return Carbon::parse($date)->format('Y-m-d h:i A');
    }
    public function getUpdatedAtAttribute($date)
    {
        return Carbon::parse($date)->format('Y-m-d h:i A');
    }

    public function answer()
    {
        return $this->hasOne(ExamAnswer::class);
    }
    public function answers()
    {
        return $this->hasMany(ExamAnswer::class);
    }
}
