<?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 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;

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', 'is_main', 'is_active',
        'manager_id', 'operating_hours',
    ];

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

    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);
    }
}
