const fs = require('fs');
const path = require('path');
const root = __dirname;
const read = p => fs.readFileSync(path.join(root,p),'utf8');
const css = read('assets/css/style.css');
const view = read('ticket_view.php');
const funcs = read('app/functions.php');
const header = read('app/header.php');
let pass=0, fail=0;
function check(name, ok){ if(ok){console.log('PASS | '+name);pass++;} else {console.error('FAIL | '+name);fail++;} }
check('Evidencija se renderuje van canManage bloka', /<\?php endif; \?>[\s\S]*?<section class="card ticket-evidence-card">/.test(view));
check('Poslednji događaji naslov ostaje prisutan', view.includes('<h3>Poslednji događaji</h3>'));
check('Timeline ostaje zajednička komponenta', view.includes('class="timeline ticket-events-timeline"'));
check('Zaduženi korisnik je canManage', /can_manage_ticket[\s\S]*assigned_to[\s\S]*user\['id'\]/.test(funcs));
check('USERF/USERDS nemaju role-specifičan audit CSS', !/\.userf-event|\.userds-event|\.ict-event/i.test(css));
check('Desktop breakpoint ostaje 1161px', css.includes('@media (min-width: 1161px)'));
check('Desktop ticket layout ostaje stretch', /\.ticket-layout\s*\{[^}]*align-items:\s*stretch/.test(css.slice(css.indexOf('v3.24.6 — stabilan desktop layout Evidencije'))));
check('Sidebar može da se uklopi u visinu glavne kartice', /\.ticket-layout > \.side-stack\s*\{[^}]*min-height:\s*0/s.test(css));
check('Evidence nema nulti flex-basis', /\.ticket-layout > \.side-stack > \.ticket-evidence-card\s*\{[^}]*flex:\s*1 1 auto/s.test(css));
check('Evidence zadržava timeline unutar svojih granica', /\.ticket-layout > \.side-stack > \.ticket-evidence-card\s*\{[^}]*overflow:\s*hidden/s.test(css));
check('Timeline zadržava interni scroll', /\.ticket-evidence-card \.ticket-events-timeline\s*\{[^}]*overflow-y:\s*auto/s.test(css));
check('Desktop timeline koristi sav raspoloživ prostor', /\.ticket-evidence-card \.ticket-events-timeline\s*\{[^}]*max-height:\s*none/s.test(css));
check('Timeline redovi se poravnavaju na početak', /\.ticket-evidence-card \.ticket-events-timeline\s*\{[^}]*align-content:\s*start/s.test(css));
check('Audit li nema fiksnu height', !/\.timeline li\s*\{[^}]*?(?:^|[;{]\s*)height\s*:/ms.test(css));
check('Audit li nema min-height', !/\.timeline li\s*\{[^}]*min-height\s*:/s.test(css));
check('Event query nema LIMIT 20', !/ORDER BY e\.created_at DESC, e\.id DESC\s+LIMIT\s+20/i.test(view));
check('Najnoviji događaj ostaje prvi', view.includes('ORDER BY e.created_at DESC, e.id DESC'));
check('Mobile/tablet ostaju jedna kolona ispod 1161px', /@media \(max-width: 1160px\)[\s\S]*?\.ticket-layout, \.user-layout \{ grid-template-columns: 1fr; \}/.test(css));
check('Nema position absolute workaround-a na Evidenciji', !/ticket-evidence-card\s*\{[^}]*position:\s*absolute/s.test(css));
check('Nema !important u desktop evidence bloku', !css.slice(css.indexOf('v3.24.6 — stabilan desktop layout Evidencije'), css.indexOf('v3.22.1 — kompaktna mobilna action dugmad')).includes('!important'));
check('Asset cache verzija je 3.25.0', (header.match(/\?v=3\.25\.0/g)||[]).length >= 2);
console.log(`RESULT ${pass}/${pass+fail} PASS`); process.exit(fail?1:0);
