<?php
namespace App\Services;

use App\Core\Database;

class TicketNumberGenerator
{
    /** Format TKT-YYYY-NNNNN (FR-SUP-005), sequential within the calendar year. */
    public static function next(): string
    {
        $year = date('Y');
        $prefix = "TKT-{$year}-";
        $max = Database::scalar(
            "SELECT MAX(CAST(SUBSTRING(ticket_no, 10) AS UNSIGNED)) FROM tickets WHERE ticket_no LIKE :p",
            ['p' => $prefix . '%']
        );
        $next = ((int)$max) + 1;
        return $prefix . str_pad((string)$next, 5, '0', STR_PAD_LEFT);
    }
}
