const fs = require('fs');
const path = require('path');
const read = (file) => fs.readFileSync(path.join(__dirname, file), 'utf8');

const files = {
  functions: read('app/functions.php'),
  equipment: read('equipment.php'),
  export: read('equipment_export.php'),
  xlsx: read('app/xlsx.php'),
  users: read('users.php'),
  header: read('app/header.php'),
  migration: read('20260823_magacioner_organization_unit.sql'),
  schema: read('baza.sql'),
  docs: read('UPGRADE_MAGACIONER_ORGANIZATION_UNIT.md'),
};

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

check('Postoje WHF i WHDS role', /'WHF'\s*=>\s*'Magacioner — Fondacija'/.test(files.functions) && /'WHDS'\s*=>\s*'Magacioner — Dečije selo'/.test(files.functions));
check('Equipment permission nije system-admin alias', files.equipment.includes('require_equipment_manager()') && !files.equipment.includes('$user = require_system_admin()'));
check('Magacioneri su izuzeti iz ticket processing rola', files.functions.includes('array_merge(base_user_role_codes(), equipment_manager_role_codes())'));
check('Centralni equipment scope koristi organization_unit', /function equipment_access_scope_sql[\s\S]*\.organization_unit = \?/.test(files.functions));
check('Lista i statistika koriste scope u SQL-u', files.equipment.includes('equipment_filter_sql($equipmentFilters, $user)'));
check('Export koristi sesijski scope', files.export.includes('require_equipment_manager()') && files.export.includes('equipment_filter_sql($filters, $user)'));
check('WH create ne veruje POST organizaciji', /elseif \(\$action === 'create'\)[\s\S]*\$organizationUnit = \(string\)\$managerOrganizationUnit/.test(files.equipment));
check('WH update zadržava organizaciju iz baze', files.equipment.includes("$oldItem['organization_unit']"));
check('Update lookup je scoped pre izmene', /SELECT e\.\*, u\.name AS user_name[\s\S]*WHERE e\.id = \? AND/.test(files.equipment));
check('Delete lookup je scoped', files.equipment.includes("equipment_access_scope_sql($user, 'e')") && files.equipment.includes("exit('Stavka opreme nije pronađena.')"));
check('Cross-entity assignment se proverava server-side', files.equipment.includes("$assignmentUser['organization_unit']") && files.equipment.includes('Oprema može biti dodeljena samo korisniku istog pravnog lica'));
check('Magacionerska rola mora odgovarati osnovnoj roli', files.users.includes('validate_warehouse_role_consistency($roleCodes)') && files.functions.includes('function validate_warehouse_role_consistency'));
check('Inventory duplicate provera koristi organizaciju', /WHERE organization_unit = \? AND inventory_no = \?/.test(files.functions));
check('Migracija uslovno dodaje composite unique', files.migration.includes('uq_equipment_org_inventory (organization_unit, inventory_no)') && files.migration.includes('@equipment_duplicate_count = 0'));
check('Migracija ne nagađa nejasne stavke', files.migration.includes('REQUIRES_MANUAL_REVIEW') && !/SET e\.organization_unit = 'F'/.test(files.migration));
check('UI prikazuje puno ime pravnog lica', files.equipment.includes('equipment_organization_unit_label'));
check('SYSTEM_ADMIN ima organization filter', /name="organization_unit"[\s\S]*<option value="">Sva<\/option>/.test(files.equipment));
check('XLSX sadrži Pravno lice', files.xlsx.includes("'Pravno lice'") && files.xlsx.includes("equipment_organization_unit_label($row['organization_unit']"));
check('Audit obuhvata ključne događaje', ['created', 'updated', 'deleted', 'assigned', 'unassigned', 'organization_unit_changed', 'inventory_no_changed'].every(action => files.equipment.includes(`'${action}'`) || files.users.includes(`'${action}'`)));
check('WH navigacija ne otvara korisnike i podešavanja', /is_system_admin\(\$user\) \|\| can_manage_equipment\(\$user\)/.test(files.header) && (files.header.match(/if \(is_system_admin\(\$user\)\)/g) || []).length >= 4);
check('Schema čuva eksplicitno pravno lice', files.schema.includes('`organization_unit` varchar(8)') && files.schema.includes('`uq_equipment_org_inventory`'));
check('Dokumentovani su rollback i rizici', files.docs.includes('## Rollback') && files.docs.includes('## Brisanje i poznati rizici'));

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

