const fs = require('fs');
const path = require('path');
const root = __dirname;
const read = (f) => fs.readFileSync(path.join(root, f), 'utf8');
const files = {
  equipment: read('equipment.php'),
  export: read('equipment_export.php'),
  functions: read('app/functions.php'),
  xlsx: read('app/xlsx.php'),
  users: read('users.php'),
  css: read('assets/css/style.css'),
  header: read('app/header.php'),
  migration: read('upgrade_v3_24_0.sql'),
};
let pass = 0, fail = 0;
function check(name, ok) { if (ok) { console.log('PASS', name); pass++; } else { console.log('FAIL', name); fail++; } }

check('Asset cache verzija 3.25.0', (files.header.match(/\?v=3\.25\.0/g) || []).length >= 2);
check('Equipment user_id je opcion u create formi', /<select name="user_id">[\s\S]*Nije dodeljeno/.test(files.equipment));
check('Create forma ima Magacin checkbox', /name="is_in_warehouse" value="1"/.test(files.equipment));
check('Edit forma ima Magacin checkbox', (files.equipment.match(/name="is_in_warehouse" value="1"/g) || []).length >= 2);
check('Backend korisnik + Magacin normalizuje Magacin na false', files.equipment.includes('$isInWarehouse = $userId === null ? $requestedWarehouse : false;'));
check('Warehouse clearuje user i datum dodele', /if \(\$isInWarehouse\)[\s\S]*\$userId = null;[\s\S]*\$assignedAt = ''/.test(files.equipment));
check('Warehouse assigned status se normalizuje na returned', /\$status === 'assigned'[\s\S]*\$status = 'returned'/.test(files.equipment));
check('Equipment query koristi LEFT JOIN users', files.equipment.includes('LEFT JOIN users u ON u.id = e.user_id'));
check('Export koristi LEFT JOIN users', files.export.includes('LEFT JOIN users u ON u.id = e.user_id'));
check('Warehouse filter je parametrizovan kroz normalizer', files.functions.includes("'warehouse' => $warehouse"));
check('Warehouse SQL filter koristi is_in_warehouse', files.functions.includes("e.is_in_warehouse = 1") && files.functions.includes("e.is_in_warehouse = 0"));
check('UI ima Magacin filter', /<label>Magacin[\s\S]*name="warehouse"/.test(files.equipment));
check('Warehouse badge koristi postojeći pill pattern', files.equipment.includes('pill equipment-warehouse'));
check('Korisničko brisanje više ne briše equipment', !files.users.includes("DELETE FROM equipment WHERE user_id = ?"));
check('Brisanje korisnika razdužuje opremu', files.users.includes('SET user_id = NULL') && files.users.includes("status = CASE WHEN status = 'assigned' THEN 'returned' ELSE status END"));
check('Assignment audit helper postoji', files.functions.includes('function record_equipment_assignment_event('));
check('Migracija user_id čini nullable', /MODIFY user_id int\(10\) UNSIGNED NULL/.test(files.migration));
check('Migracija dodaje is_in_warehouse', files.migration.includes('ADD COLUMN is_in_warehouse tinyint(1) NOT NULL DEFAULT 0'));
check('Migracija menja FK na SET NULL', files.migration.includes('ON DELETE SET NULL'));
check('Migracija dodaje warehouse index', files.migration.includes('idx_equipment_warehouse'));
check('Migracija dodaje assignment audit tabelu', files.migration.includes('CREATE TABLE equipment_assignment_events'));
check('Summary generiše jedan row mapiran po broju', files.xlsx.includes('$rowsByNumber = [];') && files.xlsx.includes('ksort($rowsByNumber, SORT_NUMERIC)'));
check('XLSX validator odbija duple/neuređene row reference', files.xlsx.includes('dupliran ili neuređen row'));
check('XLSX validator odbija NaN/undefined/null', /NaN\|Infinity\|-Infinity\|undefined\|null/.test(files.xlsx));
check('Workbook može imati Magacin sheet3', files.xlsx.includes('sheet name="Magacin"') && files.xlsx.includes("xl/worksheets/sheet3.xml"));
check('Magacin se kreira samo kada warehouse dataset nije prazan', files.xlsx.includes('$hasWarehouse = count($warehouseRows) > 0;'));
check('Sažetak uključuje Magacin count', files.xlsx.includes("xlsx_cell_inline('D' . $warehouseRow, 'Magacin'"));
check('Magacin worksheet koristi isti 12-column header', files.xlsx.includes("'Korisnik','Sektor','Lokacija','Tip opreme','Naziv opreme','Inventarski broj','Serijski broj','Status','Datum dodele','Email','Napomena','Ažurirano'"));
check('Formula-like korisnički stringovi ostaju inlineStr', files.xlsx.includes('t="inlineStr"'));
check('XML sanitizacija zadržava UTF-8 i uklanja kontrolne karaktere', files.xlsx.includes('ENT_XML1') && files.xlsx.includes('\\x0B\\x0C'));
check('CSS ne uvodi novi framework i ima lokalni warehouse stil', files.css.includes('.equipment-warehouse {') && files.css.includes('.equipment-status-stack'));
check('Equipment i export ostaju system-admin only', files.equipment.includes('require_system_admin()') && files.export.includes('require_system_admin()'));
check('Desktop equipment layout zadržava postojeće dve kolone', /\.equipment-control-grid \{[\s\S]*grid-template-columns:\s*minmax\(0, 1\.35fr\) minmax\(360px, \.65fr\)/.test(files.css));
check('Tablet layout vraća equipment kontrolu u jednu kolonu', /@media \(max-width: 1180px\)[\s\S]*\.equipment-control-grid,[\s\S]*grid-template-columns:\s*1fr/.test(files.css));
check('Mobile equipment filter ide u jednu kolonu', /@media \(max-width: 760px\)[\s\S]*\.equipment-filter-form,[\s\S]*grid-template-columns:\s*1fr/.test(files.css));
check('Warehouse checkbox nema fiksnu visinu ili absolute pozicioniranje', !/\.equipment-warehouse-check\s*\{[^}]*height\s*:/s.test(files.css) && !/\.equipment-warehouse-check\s*\{[^}]*position\s*:\s*absolute/s.test(files.css));
check('Equipment tabela zadržava lokalni horizontalni scroll', /\.equipment-table-wrap[\s\S]*overflow-x:\s*auto/.test(files.css));

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