<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Quiz extends Model
{
    protected $fillable = [
        'module_id',
        'section_id',
        'lesson_id',
        'title',
        'position',
        'passing_score',
        'duration',      // ⏱️ WAJIB
        'is_active',
    ];

    public function questions()
    {
        return $this->hasMany(QuizQuestion::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class)
            ->withPivot('score', 'is_passed')
            ->withTimestamps();
    }

    public function section()
    {
        return $this->belongsTo(Section::class);
    }

    public function lesson()
    {
        return $this->belongsTo(Lesson::class);
    }

    public function isAvailable(): bool
{
    return ! $this->available_at || now()->greaterThanOrEqualTo($this->available_at);
}

public function isExpired(): bool
{
    return $this->deadline && now()->greaterThan($this->deadline);
}

// App\Models\Quiz.php

public function attempts()
{
    return $this->hasMany(QuizAttempt::class);
}

}
