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

$passed = 0;
$failed = 0;
function qa_check(string $label, bool $condition): void
{
    global $passed, $failed;
    if ($condition) {
        $passed++;
        echo "PASS | {$label}\n";
    } else {
        $failed++;
        echo "FAIL | {$label}\n";
    }
}

$ordinary = ['id' => 10, 'is_system_admin' => 0, 'role_codes' => ['USERF']];
$ordinaryDs = ['id' => 14, 'is_system_admin' => 0, 'role_codes' => ['USERDS']];
$ict = ['id' => 11, 'is_system_admin' => 0, 'role_codes' => ['USERF', 'ICTF']];
$otherProcessor = ['id' => 12, 'is_system_admin' => 0, 'role_codes' => ['USERF', 'OFCF']];
$admin = ['id' => 13, 'is_system_admin' => 1, 'role_codes' => ['USERF']];

$show = static fn(array $user): bool => is_system_admin($user) || user_has_additional_role($user);

qa_check('Običan USERF nema dodatnu ulogu', !user_has_additional_role($ordinary));
qa_check('Običnom USERF se ne prikazuje statistika Aktivni zahtevi', !$show($ordinary));
qa_check('ICT korisnik ima dodatnu ulogu', user_has_additional_role($ict));
qa_check('ICT korisniku statistika ostaje prikazana', $show($ict));
qa_check('Druga processing uloga zadržava statistiku', $show($otherProcessor));
qa_check('Sistemski administrator zadržava statistiku', $show($admin));
qa_check('Običan USERF i dalje može da kreira tiket', can_create_ticket($ordinary));
qa_check('Običan USERDS može da kreira tiket', can_create_ticket($ordinaryDs));
qa_check('Običan USERDS nema dodatnu processing ulogu', !user_has_additional_role($ordinaryDs));

$css = file_get_contents(__DIR__ . '/assets/css/style.css');
$ticket = file_get_contents(__DIR__ . '/ticket_view.php');
qa_check('Timeline ima lokalni overflow-y auto', preg_match('/\.ticket-events-timeline\s*\{[^}]*overflow-y:\s*auto/s', $css) === 1);
qa_check('Ticket događaji nemaju LIMIT 20', strpos($ticket, 'ORDER BY e.created_at DESC, e.id DESC LIMIT 20') === false);
qa_check('Najnoviji događaj ostaje prvi', strpos($ticket, 'ORDER BY e.created_at DESC, e.id DESC') !== false);

echo "\nRezultat: {$passed}/" . ($passed + $failed) . " runtime provera uspešno.\n";
exit($failed === 0 ? 0 : 1);
