<?php

namespace App\Domain\Training\Models;

use App\Domain\Identity\Models\Branch;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Training\Enums\HolidayAffects;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Domain\Shared\Traits\BelongsToBranch;

class Holiday extends Model
{
    use BelongsToAcademy, BelongsToBranch;

    /**
     * branch_id NULL means every branch uses this row — catalogue and
     * configuration, not an operational record owned by one branch.
     */
    protected static bool $branchScopeAllowsShared = true;

    protected $fillable = [
        'academy_id', 'branch_id', 'name', 'name_ar',
        'date', 'is_recurring', 'affects',
    ];

    protected $casts = [
        'date' => 'date',
        'is_recurring' => 'boolean',
        'affects' => HolidayAffects::class,
    ];

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    public function scopeOnDate($query, $date)
    {
        return $query->where('date', $date);
    }

    public function scopeAffectsTraining($query)
    {
        return $query->whereIn('affects', [HolidayAffects::All->value, HolidayAffects::Training->value]);
    }
}
