<?php

namespace App\Models;

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

class TopicQuestion extends Model
{
    use HasFactory;
    use softDeletes;

    protected $fillable = [
        'class_id',
        'subject_id',
        'unit_id',
        'topic_id',
        'type_id',
        'difficulty_level_id',
        'question',
        'marks'
    ];
    
    protected $casts = [
        'class_id' => 'integer',
        'subject_id' => 'integer',
        'unit_id' => 'integer',
        'topic_id' => 'integer',
        'type_id' => 'integer',
        'difficulty_level_id' => 'integer',
        'marks' => 'integer',
    ];

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

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

    public function unit()
    {
        return $this->belongsTo(Unit::class, 'unit_id')->withTrashed();
    }

    public function topic()
    {
        return $this->belongsTo(Topic::class, 'topic_id')->withTrashed();
    }
    public function difficultyLevel()
    {
        return $this->belongsTo(DifficultyLevel::class, 'difficulty_level_id','id');
    }

    public function type()
    {
        return $this->belongsTo(TopicQuestionType::class, 'type_id','id');
    }

    public function level()
    {
        return $this->belongsTo(DifficultyLevel::class, 'difficulty_level_id','id');
    }


    // ****************************** ONE TO MANY **************************
    public function options()
    {
        return $this->hasMany(TopicQuestionOption::class, 'question_id');
    }

    public function image()
    {
        return $this->hasOne(TopicQuestionImage::class, 'question_id');
    }

    public function comment()
    {
        return $this->hasOne(TopicQuestionComment::class, 'question_id');
    }

    public function answer()
    {
        return $this->hasOne(TopicQuestionAnswer::class, 'question_id');
    }

    public function questionParts()
    {
        return $this->hasMany(TopicQuestionPart::class, 'question_id');
    }
}
