<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Models;

use App\Core\Model;
use App\Core\App;

class LockerRental extends Model
{
    protected static string $table = 'sa_locker_rentals';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static array $fillable = [
        'rental_number', 'locker_id', 'player_id', 'rental_type',
        'start_date', 'end_date', 'amount', 'payment_status',
        'payment_id', 'receipt_id', 'receipt_number', 'status',
        'grace_started_at', 'evicted_at', 'eviction_reason', 'eviction_by',
        'renewed_from_id', 'notes',
    ];

    public static function getRentalTypeOptions(): array
    {
        return [
            'monthly'  => 'شهري',
            '6_months' => '6 شهور',
            'yearly'   => 'سنوي',
        ];
    }

    public static function getStatusOptions(): array
    {
        return [
            'active'            => 'فعال',
            'grace_period'      => 'فترة سماح',
            'pending_eviction'  => 'بانتظار الإخلاء',
            'evicted'           => 'تم الإخلاء',
            'completed'         => 'مكتمل',
            'cancelled'         => 'ملغي',
        ];
    }

    public static function generateNumber(): string
    {
        $db = App::getInstance()->db();
        $prefix = 'LR-' . date('Ym') . '-';
        $last = $db->selectOne(
            "SELECT rental_number FROM sa_locker_rentals WHERE rental_number LIKE ? ORDER BY id DESC LIMIT 1",
            [$prefix . '%']
        );
        if ($last) {
            $seq = (int) substr($last['rental_number'], -4) + 1;
        } else {
            $seq = 1;
        }
        return $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
    }
}
