<?php

namespace App\Domain\Facility\Models;

use App\Domain\Facility\Enums\FacilityStatus;
use App\Domain\Facility\Enums\FacilityType;
use App\Domain\Identity\Models\Branch;
use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Domain\Shared\Traits\ManglesUniqueOnDelete;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Facility extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, Auditable, ManglesUniqueOnDelete;

    protected array $uniqueFieldsToMangle = ['code'];

    protected $fillable = [
        'academy_id',
        'branch_id',
        'name',
        'name_ar',
        'code',
        'type',
        'status',
        'description',
        'description_ar',
        'capacity',
        'surface_type',
        'area_sqm',
        'length_m',
        'width_m',
        'is_indoor',
        'has_lighting',
        'has_ac',
        'operating_start',
        'operating_end',
        'rental_cost_per_hour',
        'monthly_rental_cost',
        'address',
        'latitude',
        'longitude',
        'amenities',
        'metadata',
        'photo_path',
        'sort_order',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'type' => FacilityType::class,
            'status' => FacilityStatus::class,
            'capacity' => 'integer',
            'area_sqm' => 'decimal:2',
            'length_m' => 'decimal:2',
            'width_m' => 'decimal:2',
            'is_indoor' => 'boolean',
            'has_lighting' => 'boolean',
            'has_ac' => 'boolean',
            'rental_cost_per_hour' => 'integer',
            'monthly_rental_cost' => 'integer',
            'amenities' => 'array',
            'metadata' => 'array',
            'sort_order' => 'integer',
        ];
    }

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

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function layouts(): HasMany
    {
        return $this->hasMany(SpaceLayout::class);
    }

    public function activeLayout(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne(SpaceLayout::class)->where('is_active', true)->orderBy('sort_order');
    }

    public function reservations(): HasMany
    {
        return $this->hasMany(SpaceReservation::class);
    }

    /**
     * Sports this facility can host. Empty = hosts anything (the default for
     * facilities created before the link existed).
     */
    public function activities(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(
            \App\Domain\Training\Models\Activity::class,
            'facility_activities',
            'facility_id',
            'activity_id'
        )->withTimestamps();
    }

    /** Facilities belonging to one branch. */
    public function scopeForBranch($query, ?int $branchId)
    {
        return $branchId ? $query->where('branch_id', $branchId) : $query;
    }

    public function scopeActive($query)
    {
        return $query->where('status', FacilityStatus::Active);
    }
}
