<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\Booking\BookingService;
use Illuminate\Console\Command;

/**
 * Settles the status of bookings whose payment hold has run out.
 *
 * Deliberately not load-bearing. Availability already ignores a lapsed hold
 * and BookingService clears stale rows out of the way when someone rebooks
 * the time, so a slot frees itself the instant its countdown ends whether or
 * not this ever runs.
 *
 * That matters because the client's shared cPanel hosting caps cron at every
 * five minutes on some plans, and fifteen on others. If slot release depended
 * on this command firing promptly, the studio would quietly stop selling
 * whenever cron slipped. Here, a late run only means the admin panel shows a
 * stale status for a few minutes and the release email goes out late.
 */
class ExpireBookingHolds extends Command
{
    protected $signature = 'booking:expire-holds';

    protected $description = 'Mark bookings whose payment hold has lapsed as expired and release their slots';

    public function handle(BookingService $bookings): int
    {
        $expired = $bookings->expireLapsedHolds();

        $this->info($expired === 0
            ? 'No lapsed holds.'
            : "Expired {$expired} lapsed ".str('hold')->plural($expired).'.');

        return self::SUCCESS;
    }
}
