<?php
declare(strict_types=1);

require_once __DIR__ . '/db.php';

function generateInvoiceNumber(PDO $db): string
{
    $year = date('Y');

    $stmt = $db->prepare("
        SELECT COUNT(*) + 1
        FROM invoices
        WHERE invoice_number LIKE :prefix
    ");
    $stmt->execute([
        'prefix' => "INV-$year-%"
    ]);

    $sequence = str_pad((string)$stmt->fetchColumn(), 6, '0', STR_PAD_LEFT);

    return "INV-$year-$sequence";
}
