<?php

namespace App\Models;

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

class StudentQuizAnswer extends Model
{
    use HasFactory;

    protected $table = 'student_quiz_answers';
    protected $fillable = [
        'student_id',
        'teacher_class_id',
        'quiz_id',
        'student_quiz_id',
        'question_id',
        'answer',
        'outcome',
        'score',
        'marks',
        'total_marks',
        'reason'
    ];
    
    protected $casts = [
        'student_id' => 'integer',
        'teacher_class_id' => 'integer',
        'quiz_id' => 'integer',
        'student_quiz_id' => 'integer',
        'question_id' => 'integer',
        'outcome' => 'integer',
        'marks' => 'integer',
        'total_marks' => 'integer',
    ];

    public function studentQuiz()
    {
        return $this->belongsTo(StudentQuiz::class, 'student_quiz_id', 'id');
    }

    // Relationship with QuizTopicQuestion
    public function question()
    {
        return $this->belongsTo(QuizTopicQuestion::class, 'question_id', 'id');
    }

    // Relationship with StudentRegister
    public function student()
    {
        return $this->belongsTo(StudentRegister::class, 'student_id', 'id');
    }

    // Relationship with TeacherClass
    public function teacherClass()
    {
        return $this->belongsTo(TeacherClass::class, 'teacher_class_id', 'id');
    }

    public function quiz()
    {
        return $this->belongsTo(Quiz::class);
    }
    
    public function answerParts()
    {
        return $this->hasMany(StudentQuizAnswerPart::class, 'student_quiz_answer_id');
    }
}
