<?php

namespace App\Domain\Identity\Models;

use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Domain\Shared\Traits\ManglesUniqueOnDelete;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;

class Branch extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, ManglesUniqueOnDelete;

    protected array $uniqueFieldsToMangle = ['code'];

    protected $fillable = [
        'academy_id', 'name', 'name_ar', 'code',
        'address', 'city', 'governorate', 'phone', 'email',
        'latitude', 'longitude', 'photo_path',
        'season_starts_on', 'season_ends_on',
        'is_main', 'is_active',
        'manager_id', 'operating_hours',
    ];

    protected $casts = [
        'is_main' => 'boolean',
        'is_active' => 'boolean',
        'latitude' => 'decimal:7',
        'longitude' => 'decimal:7',
        'season_starts_on' => 'date',
        'season_ends_on' => 'date',
        'operating_hours' => 'array',
    ];

    /**
     * Does the branch have a declared season at all?
     *
     * A branch with no window is open all year — which is what every branch was
     * before the season columns existed, so the absence of a window must never
     * read as "closed".
     */
    public function hasSeasonWindow(): bool
    {
        return $this->season_starts_on !== null || $this->season_ends_on !== null;
    }

    /**
     * Is the branch inside its season on the given day?
     *
     * An open-ended window counts as open on that side: a start with no end is
     * "from this date onwards", an end with no start is "until this date".
     */
    public function isInSeason(?DateTimeInterface $on = null): bool
    {
        if (! $this->hasSeasonWindow()) {
            return true;
        }

        $day = ($on ? Carbon::instance($on) : now())->startOfDay();

        if ($this->season_starts_on && $day->lt($this->season_starts_on->startOfDay())) {
            return false;
        }

        if ($this->season_ends_on && $day->gt($this->season_ends_on->startOfDay())) {
            return false;
        }

        return true;
    }

    public function manager(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'manager_id');
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(\App\Models\User::class, 'role_user', 'branch_id', 'user_id');
    }

    public function participants(): HasMany
    {
        return $this->hasMany(\App\Domain\Participant\Models\Participant::class);
    }

    public function groups(): HasMany
    {
        return $this->hasMany(\App\Domain\Training\Models\TrainingGroup::class);
    }

    public function facilities(): HasMany
    {
        return $this->hasMany(\App\Domain\Facility\Models\Facility::class);
    }

    public function cashSessions(): HasMany
    {
        return $this->hasMany(\App\Domain\Financial\Models\CashSession::class);
    }
}
