<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LockerGroup extends Model
{
    protected $fillable = [
        'row_count',
        'column_count',
        'name',
        'description',
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the lockers for this locker group.
     */
    public function lockers(): HasMany
    {
        return $this->hasMany(Locker::class);
    }

    /**
     * Get the total number of lockers in this group.
     */
    public function getTotalLockersAttribute(): int
    {
        return $this->row_count * $this->column_count;
    }

    /**
     * Get the count of available lockers in this group.
     */
    public function getAvailableLockersCountAttribute(): int
    {
        return $this->lockers()->where('status', 'Available')->count();
    }

    /**
     * Get the count of occupied lockers in this group.
     */
    public function getOccupiedLockersCountAttribute(): int
    {
        return $this->lockers()->where('status', 'Occupied')->count();
    }

    /**
     * Get the count of lockers under maintenance in this group.
     */
    public function getMaintenanceLockersCountAttribute(): int
    {
        return $this->lockers()->where('status', 'Maintenance')->count();
    }

    /**
     * Get the count of reserved lockers in this group.
     */
    public function getReservedLockersCountAttribute(): int
    {
        return $this->lockers()->where('status', 'Reserved')->count();
    }

    /**
     * Get the count of blocked lockers in this group.
     */
    public function getBlockedLockersCountAttribute(): int
    {
        return $this->lockers()->where('status', 'Blocked')->count();
    }

    /**
     * Get the utilization percentage of this locker group.
     */
    public function getUtilizationPercentageAttribute(): float
    {
        $total = $this->getTotalLockersAttribute();
        if ($total === 0) {
            return 0;
        }
        
        $occupied = $this->getOccupiedLockersCountAttribute();
        return round(($occupied / $total) * 100, 2);
    }
}
