<?php

namespace App\Models;

use App\Models\Traits\BelongsToScoala;
use App\Models\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Model;

class Clasa extends Model
{
    use BelongsToScoala, LogsActivity;

    protected $table = 'clase';

    protected $fillable = [
        'scoala_id',
        'diriginte_user_id',
        'nume',
        'an_scolar',
        'tip_perioada',
        'notificari_email',
        'notificari_whatsapp',
        'orar_finalizat',
        'cu_teze',
    ];

    protected $casts = [
        'notificari_email' => 'boolean',
        'notificari_whatsapp' => 'boolean',
        'orar_finalizat' => 'boolean',
        'cu_teze' => 'boolean',
    ];

    public function emailActiv(): bool
    {
        if ($this->notificari_email !== null) {
            return $this->notificari_email;
        }
        return $this->scoala?->notificari_email ?? false;
    }

    public function whatsappActiv(): bool
    {
        if ($this->notificari_whatsapp !== null) {
            return $this->notificari_whatsapp;
        }
        return $this->scoala?->notificari_whatsapp ?? false;
    }

    public function diriginte()
    {
        return $this->belongsTo(User::class, 'diriginte_user_id');
    }

    public function elevi()
    {
        return $this->hasMany(Elev::class);
    }

    public function materii()
    {
        return $this->hasMany(Materie::class);
    }

    public function perioade()
    {
        return $this->hasMany(Perioada::class)->orderBy('ordine');
    }

    public function orarSlots()
    {
        return $this->hasMany(OrarSlot::class);
    }
}