<?php
declare(strict_types=1);
if (!function_exists('mb_substr')) { function mb_substr(string $s, int $start, ?int $length = null): string { return $length === null ? substr($s, $start) : substr($s, $start, $length); } }
require_once __DIR__ . '/app/functions.php';
require_once __DIR__ . '/app/xlsx.php';

$pass = 0;
$fail = 0;
function qa_check(string $name, bool $condition): void {
    global $pass, $fail;
    if ($condition) {
        echo "PASS $name\n";
        $pass++;
    } else {
        echo "FAIL $name\n";
        $fail++;
    }
}

$filters = normalize_equipment_filters(['asset_type' => 'Docking station', 'status' => 'repair', 'q' => 'Vesna']);
qa_check('Legacy detaljni tip se mapira u Ostalo filter', $filters['asset_type'] === 'Ostalo');
qa_check('Validan status ostaje', $filters['status'] === 'repair');
qa_check('Search ostaje sačuvan', $filters['q'] === 'Vesna');

[$whereOther, $paramsOther] = equipment_filter_sql($filters);
qa_check('Ostalo SQL koristi NOT IN glavnih tipova', str_contains($whereOther, "e.asset_type NOT IN ('Laptop', 'Telefon', 'Monitor', 'Tablet')"));
qa_check('Search koristi osam parametrizovanih polja', substr_count($whereOther, 'LOCATE(?,') === 8);
qa_check('Parametri sadrže status + 8 search vrednosti', count($paramsOther) === 9 && $paramsOther[0] === 'repair');

[$whereLaptop, $paramsLaptop] = equipment_filter_sql(normalize_equipment_filters(['asset_type' => 'Laptop', 'user_id' => '7']));
qa_check('Laptop filter je tačan equality filter', str_contains($whereLaptop, 'e.asset_type = ?'));
qa_check('User + type parametri su parametrizovani', $paramsLaptop === [7, 'Laptop']);
qa_check('Summary grupiše Docking station u Ostalo', equipment_summary_type('Docking station') === 'Ostalo');
qa_check('Summary zadržava Tablet kao Tablet', equipment_summary_type('Tablet') === 'Tablet');

qa_check('Pagination window početak', pagination_page_numbers(1, 8) === [1,2,3,null,8]);
qa_check('Pagination window sredina', pagination_page_numbers(4, 8) === [1,2,3,4,5,6,null,8]);
qa_check('Pagination nema UI za jednu stranicu', pagination_page_numbers(1, 1) === []);
qa_check('0 korisnika ostaje jedna prazna stranica bez pagination linkova', pagination_page_numbers(1, max(1, (int)ceil(0 / 10))) === []);
qa_check('11 korisnika daje dve stranice', pagination_page_numbers(1, max(1, (int)ceil(11 / 10))) === [1,2]);
qa_check('100+ korisnika daje skalabilan pagination prozor', pagination_page_numbers(6, max(1, (int)ceil(108 / 10))) === [1,null,4,5,6,7,8,null,11]);

$rows = [
    [
        'user_name' => 'Test Testić', 'user_email' => 'test@sos-decijasela.rs', 'user_department' => null, 'user_location' => null,
        'asset_type' => 'Telefon', 'asset_name' => 'Honor', 'inventory_no' => '0000123', 'serial_no' => '=CMD|TEST',
        'status' => 'assigned', 'assigned_at' => '2026-07-10', 'notes' => '=1+1 formula test', 'updated_at' => '2026-08-20 19:46:00'
    ],
    [
        'user_name' => 'Vesna Lekić', 'user_email' => 'vesna.lekic@sos-decijasela.rs', 'user_department' => 'Finansije', 'user_location' => 'Beograd',
        'asset_type' => 'Docking station', 'asset_name' => 'HP Dock G5', 'inventory_no' => 'INV-2', 'serial_no' => 'SER-2',
        'status' => 'repair', 'assigned_at' => '2026-06-01', 'notes' => '', 'updated_at' => '2026-08-20 10:00:00'
    ],
];
$path = create_equipment_xlsx($rows, new DateTimeImmutable('2026-08-20 19:46:00', new DateTimeZone('Europe/Belgrade')));
qa_check('XLSX fajl postoji', is_file($path) && filesize($path) > 1000);

try {
    $zip = new PharData($path);
    $sheet1 = $zip['xl/worksheets/sheet1.xml']->getContent();
    $sheet2 = $zip['xl/worksheets/sheet2.xml']->getContent();
    $workbook = $zip['xl/workbook.xml']->getContent();
    qa_check('Workbook sadrži oba worksheet naziva', str_contains($workbook, 'Pregled opreme') && str_contains($workbook, 'Sažetak'));
    qa_check('Glavni sheet sadrži 12 traženih headera', str_contains($sheet1, 'Inventarski broj') && str_contains($sheet1, 'Serijski broj') && str_contains($sheet1, 'Ažurirano'));
    qa_check('Leading zero inventarski broj ostaje tekst', str_contains($sheet1, '>0000123<'));
    qa_check('Formula-like serial ostaje literalni tekst', str_contains($sheet1, '>=CMD|TEST<'));
    qa_check('Formula-like napomena ostaje literalni tekst', str_contains($sheet1, '>=1+1 formula test<'));
    qa_check('Sažetak grupiše ne-glavni tip u Ostalo', str_contains($sheet2, '>Ostalo<'));
    qa_check('Sažetak sadrži lokacijski fallback', str_contains($sheet2, '>Nije navedeno<'));
} catch (Throwable $e) {
    qa_check('XLSX ZIP može da se otvori', false);
}
@unlink($path);

echo 'RESULT ' . $pass . '/' . ($pass + $fail) . " PASS\n";
exit($fail ? 1 : 0);
