<?php

namespace App\Models;

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

class TeacherClass extends Model
{
    use HasFactory;
    protected $table = 'teacher_classes';

    protected $fillable = [
        'teacher_id',
        'class_id',
        'year',
        'section',
        'subject_id',
        'status'
    ];
    
    protected $casts = [
        'teacher_id' => 'integer',
        'class_id' => 'integer',
        'subject_id' => 'integer',
    ];

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

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

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

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

    public function students()
    {
        return $this->hasMany(StudentClass::class, 'teacher_class_id');
    }
    
    public function studentsCount()
    {
        return $this->studentClasses()->count();
    }

    public function subjects()
    {
        return $this->hasMany(Subject::class, 'subject_id', 'id')->withTrashed();
    }
}
