const fs = require('fs');
const path = require('path');

const root = __dirname;
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
const css = read('assets/css/style.css');
const equipment = read('equipment.php');
const header = read('app/header.php');

let passed = 0;
let failed = 0;
function check(name, condition) {
  if (condition) {
    console.log('PASS', name);
    passed++;
  } else {
    console.log('FAIL', name);
    failed++;
  }
}

check('Equipment tabela zadržava svih devet operativnih kolona',
  /<th>Korisnik<\/th>[\s\S]*<th>Napomena<\/th>[\s\S]*<th>Ažurirano<\/th>[\s\S]*<th>Akcije<\/th>/.test(equipment));
check('Equipment wrapper vraća lokalni overflow',
  /\.equipment-table-wrap\s*\{[^}]*overflow:\s*auto/s.test(css));
check('Stari grouped overflow-visible override je uklonjen',
  !/\.equipment-table-card\s*,\s*\.equipment-table-wrap\s*\{[^}]*overflow:\s*visible\s*!important/s.test(css));
check('Desktop equipment tabela koristi kontrolisanu minimalnu širinu',
  /@media \(min-width:\s*761px\)[\s\S]*?\.equipment-table\s*\{[^}]*min-width:\s*1070px[^}]*table-layout:\s*fixed/s.test(css));
check('Svih devet desktop kolona ima sadržajno prilagođenu širinu',
  Array.from({ length: 9 }, (_, index) => new RegExp(`\\.equipment-table th:nth-child\\(${index + 1}\\)\\s*\\{[^}]*width:`).test(css)).every(Boolean));
check('Akcije imaju širinu za dugme i obostrani padding',
  /\.equipment-table th:nth-child\(9\)\s*\{[^}]*width:\s*115px/s.test(css)
  && /\.equipment-table th:last-child,\s*\.equipment-table td:last-child\s*\{[^}]*padding-right:\s*12px[^}]*padding-left:\s*10px/s.test(css));
check('Uredi dugmad imaju stabilne dimenzije i centriranje',
  /\.equipment-table \.equipment-row-editor summary\s*\{[^}]*width:\s*76px[^}]*min-height:\s*38px[^}]*margin:\s*0 auto/s.test(css));
check('Ikonice opreme ne mogu da se skupe u flex redu',
  /\.equipment-table \.equipment-icon\s*\{[^}]*flex:\s*0 0 48px[^}]*min-width:\s*48px[^}]*height:\s*48px/s.test(css));
check('Primarna i sekundarna tipografija tabele je eksplicitno ujednačena',
  /\.equipment-table td > strong,[\s\S]*?font-size:\s*14px[^}]*line-height:\s*1\.45/s.test(css)
  && /\.equipment-table td small,[\s\S]*?font-size:\s*12px[^}]*line-height:\s*1\.4/s.test(css));
check('Dugačak tekst u ćelijama može da se prelomi',
  /\.equipment-table th,\s*\.equipment-table td\s*\{[^}]*overflow-wrap:\s*anywhere/s.test(css));
check('Laptop action kolona ostaje sticky i dostupna',
  /@media \(min-width:\s*761px\) and \(max-width:\s*1535px\)[\s\S]*?\.equipment-table th:last-child,[\s\S]*?position:\s*sticky[^}]*right:\s*0/s.test(css));
check('Mobilni editor zadržava postojeći statični raspored',
  /@media \(max-width:\s*760px\)[\s\S]*?\.equipment-row-form\s*\{[^}]*position:\s*static[^}]*width:\s*100%/s.test(css));
check('Podešavanje zahteva prelazi u jednu kolonu na laptop desktop širinama',
  /@media \(max-width:\s*1760px\)\s*\{[\s\S]*?\.ticket-settings-grid\s*\{[^}]*grid-template-columns:\s*1fr/s.test(css));
check('Full HD zadržava osnovni dvokolonski settings raspored',
  /\.ticket-settings-grid\s*\{[^}]*grid-template-columns:\s*repeat\(2,\s*minmax\(0,\s*1fr\)\)/s.test(css));
check('Glavni sadržaj može da se skupi bez globalnog overflow-a',
  /\.sadrzaj\s*\{[^}]*min-width:\s*0/s.test(css));
check('Novi CSS dobija poseban cache ključ',
  header.includes('?v=3.25.0&amp;layout=3.25.3'));

for (const page of ['dashboard.php', 'equipment.php', 'tickets.php', 'users.php']) {
  check(`${page} drži tabelu u lokalnom table-wrap kontejneru`,
    /<div class="[^"]*table-wrap[^"]*">\s*<table/s.test(read(page)));
}

console.log(`RESULT ${passed}/${passed + failed} PASS`);
process.exit(failed ? 1 : 0);
