<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Quiz extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'quizzes';

    protected $fillable = [
        'teacher_id',
        'teacher_class_id',
        'class_id',
        'section',
        'subject_id',
        'quiz_name',
        'quiz_question_order',
        'quiz_message',
        'is_publish',
        'is_active',
        'start_date',
        'end_date',
        'total_marks',
        'total_time'
    ];
    
    protected $casts = [
        'teacher_id' => 'integer',
        'teacher_class_id' => 'integer',
        'class_id' => 'integer',
        'subject_id' => 'integer',
        'is_publish' => 'integer',
        'is_active' => 'integer',
        'total_marks' => 'integer',
    ];

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

    public function classRelation()
    {
        return $this->belongsTo(Classes::class, 'class_id')->withTrashed();
    }

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

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

    // Quiz Relation with multiple Students

    public function studentRegisters()
    {
        return $this->hasMany(StudentRegister::class, 'student_id', 'id');
    }

    public function students()
    {
        return $this->hasMany(QuizStudent::class, 'quiz_id', 'id');
    }
    
    public function teacherClass()
    {
        return $this->belongsTo(TeacherClass::class, 'teacher_class_id', 'id');
    }

    // Quiz Relation with multiple units
    public function units()
    {
        return $this->hasMany(QuizUnit::class, 'quiz_id', 'id')->withTrashed();
    }

    // Quiz Relation with multiple Topics
    public function topics()
    {
        return $this->hasMany(QuizTopic::class, 'quiz_id', 'id')->withTrashed();
    }

    // Quiz Relation with multiple Link
    public function links()
    {
        return $this->hasMany(QuizLink::class, 'quiz_id', 'id')->withTrashed();
    }

    public function questions()
    {
        return $this->hasMany(QuizTopicQuestion::class, 'question_id', 'id')->withTrashed();
    }
}
