<?php
/**
 * Helper API – returns the next auto-generated registration number
 */
header('Content-Type: application/json');

session_set_cookie_params([
    'lifetime' => 0, 'path' => '/', 'domain' => '',
    'secure'   => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
    'httponly' => true, 'samesite' => 'Lax'
]);
session_start();

if (!isset($_SESSION['userid'])) {
    http_response_code(401);
    echo json_encode(['success' => false]);
    exit();
}

require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');
require_once('../libs/classes/MyMethods.php');

$crud   = new App_DB(DBU::$dba_user);
$method = new MyMethods();

$count = (int)$crud->getValue("SELECT COUNT(sno) FROM regnumbers");
$next  = $method->changeNumberFormate($count + 1);

echo json_encode(['success' => true, 'nextRegNo' => $next]);
