<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Locker extends Model
{
    protected $fillable = [
        'locker_group_id',
        'locker_number',
        'locker_size',
        'locker_type',
        'locker_row_column',
        'locker_amount',
        'status',
        'location_id',
        'customer_id',
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the locker group that owns the locker.
     */
    public function lockerGroup(): BelongsTo
    {
        return $this->belongsTo(LockerGroup::class);
    }

    /**
     * Get the location that owns the locker.
     */
    public function location(): BelongsTo
    {
        return $this->belongsTo(ClientLocation::class);
    }

    /**
     * Get the customer that owns the locker.
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'customer_id');
    }

    /**
     * Get the locker registrations for this locker.
     */
    public function lockerRegistrations(): HasMany
    {
        return $this->hasMany(LockerRegistration::class);
    }

    public function registrationItems()
    {
        return $this->hasMany(LockerRegistrationItem::class, 'locker_id');
    }

    /**
     * Get active registration for this locker.
     */
    public function activeRegistration()
    {
        return $this->hasOne(LockerRegistration::class)
            ->where('status', 'Active');
    }

    /**
     * Scope to get available lockers.
     */
    public function scopeAvailable($query)
    {
        return $query->where('status', 'available');
    }

    /**
     * Scope to get occupied lockers.
     */
    public function scopeOccupied($query)
    {
        return $query->where('status', 'occupied');
    }

    /**
     * Scope to get lockers under maintenance.
     */
    public function scopeMaintenance($query)
    {
        return $query->where('status', 'maintenance');
    }

    /**
     * Scope to get reserved lockers.
     */
    public function scopeReserved($query)
    {
        return $query->where('status', 'reserved');
    }

    /**
     * Scope to get blocked lockers.
     */
    public function scopeBlocked($query)
    {
        return $query->where('status', 'blocked');
    }

    /**
     * Scope to filter by size.
     */
    public function scopeBySize($query, $size)
    {
        return $query->where('locker_size', $size);
    }

    /**
     * Scope to filter by type.
     */
    public function scopeByType($query, $type)
    {
        return $query->where('locker_type', $type);
    }

    /**
     * Scope to filter by location.
     */
    public function scopeByLocation($query, $locationId)
    {
        return $query->where('location_id', $locationId);
    }

    /**
     * Scope to filter by expiry.
     */
    public function scopeExpiry($query, $expiryFrom, $expiryTo)
    {
        return $query->whereHas('activeRegistration', function ($q) use ($expiryFrom, $expiryTo) {
            $q->whereBetween('expiry_to', [$expiryFrom, $expiryTo]);
        });
    }
    /**
     * Get status color for UI.
     */
    public function getStatusColorAttribute()
    {
        return match ($this->status) {
            'available' => 'green',
            'reserved' => 'blue',
            'occupied' => 'red',
            'maintenance' => 'yellow',
            'blocked' => 'gray',
            default => 'gray',
        };
    }

    /**
     * Get status icon for UI.
     */
    public function getStatusIconAttribute()
    {
        return match ($this->status) {
            'available' => 'door-open',
            'reserved' => 'clock',
            'occupied' => 'lock',
            'maintenance' => 'tools',
            'blocked' => 'ban',
            default => 'door-closed',
        };
    }
}
