<?php
require_once __DIR__ . '/app/functions.php';

$mode = $argv[1] ?? 'normal';
$baseUser = ['id' => 10, 'is_system_admin' => 0, 'role_codes' => ['USERF']];
$baseDsUser = ['id' => 12, 'is_system_admin' => 0, 'role_codes' => ['USERDS']];
$additionalUser = ['id' => 10, 'is_system_admin' => 0, 'role_codes' => ['USERF', 'VHCF']];
$adminUser = ['id' => 1, 'is_system_admin' => 1, 'role_codes' => ['USERF']];

if ($mode === 'unauthorized-assigned') {
    register_shutdown_function(static function (): void {
        fwrite(STDERR, "\nHTTP_STATUS=" . http_response_code() . "\n");
    });
    ticket_list_view($baseUser, 'assigned');
}

if ($mode === 'unauthorized-all') {
    register_shutdown_function(static function (): void {
        fwrite(STDERR, "\nHTTP_STATUS=" . http_response_code() . "\n");
    });
    ticket_list_view($baseUser, 'all');
}

if ($mode === 'invalid-array') {
    register_shutdown_function(static function (): void {
        fwrite(STDERR, "\nHTTP_STATUS=" . http_response_code() . "\n");
    });
    ticket_list_view($baseUser, ['assigned']);
}

$checks = [];
$assert = static function (string $name, bool $condition) use (&$checks): void {
    $checks[] = [$name, $condition];
};

$assert('USERF nema dodatnu ulogu', user_has_additional_role($baseUser) === false);
$assert('USERDS nema dodatnu ulogu', user_has_additional_role($baseDsUser) === false);
$assert('USERDS može da kreira tiket', can_create_ticket($baseDsUser) === true);
$assert('USERF + VHCF ima dodatnu ulogu', user_has_additional_role($additionalUser) === true);
$assert('Podrazumevani prikaz običnog korisnika je mine', ticket_list_view($baseUser, null) === 'mine');
$assert('Podrazumevani prikaz administratora je all', ticket_list_view($adminUser, null) === 'all');
$assert('Dozvoljen assigned prikaz dodatnoj ulozi', ticket_list_view($additionalUser, 'assigned') === 'assigned');
$assert('Dozvoljen all prikaz administratoru', ticket_list_view($adminUser, 'all') === 'all');

[$mineSql, $mineParams] = my_tickets_access_sql($baseUser);
$assert('Mine SQL sadrži podnosioca', str_contains($mineSql, 't.user_id = ?'));
$assert('Mine SQL sadrži direktnu dodelu', str_contains($mineSql, 't.assigned_to = ?'));
$assert('Mine SQL sadrži participant EXISTS', str_contains($mineSql, 'tp_mine.user_id = ?'));
$assert('Mine SQL koristi isti user ID za sva tri odnosa', $mineParams === [10, 10, 10]);

[$assignedSql, $assignedParams] = assigned_tickets_access_sql($additionalUser);
$assert('Assigned SQL sadrži direktnu dodelu', str_contains($assignedSql, 't.assigned_to = ?'));
$assert('Assigned SQL sadrži role EXISTS', str_contains($assignedSql, 'r_assigned.role_code = t.responsible_role_code'));
$assert('Assigned SQL isključuje obe osnovne role', $assignedParams === [10, 10, 'USERF', 'USERDS']);

$failed = 0;
foreach ($checks as [$name, $ok]) {
    echo ($ok ? 'PASS' : 'FAIL') . ' | ' . $name . PHP_EOL;
    if (!$ok) {
        $failed++;
    }
}

echo PHP_EOL . 'Rezultat: ' . (count($checks) - $failed) . '/' . count($checks) . ' runtime provera uspešno.' . PHP_EOL;
exit($failed === 0 ? 0 : 1);
