const fs = require('fs');

let appFinalJs = fs.readFileSync('app_final.js', 'utf8');

// 1. Fix warehouse filter logic (String vs Integer issue)
appFinalJs = appFinalJs.replace(
    /if \(currentUser\.role === 'أمين مخزن' && currentUser\.warehouse_id\) \{[\s\S]*?filtered = filtered\.filter\(t => currentUser\.allowed_warehouses\.includes\(t\.warehouse_id\) \|\| currentUser\.allowed_warehouses\.includes\(t\.to_warehouse_id\)\);\s*\}/g,
    `if (currentUser.role === 'أمين مخزن' && currentUser.warehouse_id) {
    filtered = filtered.filter(t => String(t.warehouse_id) === String(currentUser.warehouse_id) || String(t.to_warehouse_id) === String(currentUser.warehouse_id));
  } else if (currentUser.role === 'موظف مخصص' && currentUser.allowed_warehouses) {
    filtered = filtered.filter(t => currentUser.allowed_warehouses.some(w => String(w) === String(t.warehouse_id) || String(w) === String(t.to_warehouse_id)));
  }`
);

// 2. Fix missing types in renderTransactions
const missingTypes = `else if (t.type === 'sales') { typeLabel = 'فاتورة مبيعات'; whLabel = t.from_warehouse_name || (currentWarehouses.find(w=>String(w.id)===String(t.warehouse_id))?.name) || '-'; }
    else if (t.type === 'purchases') { typeLabel = 'فاتورة مشتريات'; whLabel = t.to_warehouse_name || (currentWarehouses.find(w=>String(w.id)===String(t.to_warehouse_id))?.name) || '-'; }
    else if (t.type === 'sales_return') { typeLabel = 'مرتجع مبيعات'; whLabel = t.to_warehouse_name || (currentWarehouses.find(w=>String(w.id)===String(t.to_warehouse_id))?.name) || '-'; }
    else if (t.type === 'purchases_return') { typeLabel = 'مرتجع مشتريات'; whLabel = t.from_warehouse_name || (currentWarehouses.find(w=>String(w.id)===String(t.warehouse_id))?.name) || '-'; }
    else if (t.type === 'petty_cash_settlement') { typeLabel = 'تسوية عهدة'; whLabel = t.to_warehouse_name || (currentWarehouses.find(w=>String(w.id)===String(t.to_warehouse_id))?.name) || '-'; }`;

appFinalJs = appFinalJs.replace(
    /else if \(t\.type === 'transfer'\) \{ typeLabel = 'تحويل مخزني'; whLabel = `من \(\$\{t\.from_warehouse_name\}\) إلى \(\$\{t\.to_warehouse_name\}\)`; \}/g,
    `else if (t.type === 'transfer') { typeLabel = 'تحويل مخزني'; whLabel = \`من (\${t.from_warehouse_name}) إلى (\${t.to_warehouse_name})\`; }\n    ${missingTypes}`
);

// Add missing statuses
appFinalJs = appFinalJs.replace(
    /else if \(t\.status === 'pending_decision'\) statusLabel = '<span style="color:var\(--danger\)">يوجد عجز<\/span>';/g,
    `else if (t.status === 'pending_decision') statusLabel = '<span style="color:var(--danger)">يوجد عجز</span>';
    else if (t.status === 'posted') statusLabel = '<span style="color:var(--success)">مُرحلة</span>';
    else if (t.status === 'approved') statusLabel = '<span style="color:var(--accent)">موافق عليها</span>';`
);

// 3. Fix setupUserUI navigation for أمين مخزن (adding contacts, vouchers, settlements)
appFinalJs = appFinalJs.replace(
    /document\.getElementById\('nav-purchases'\)\.style\.display = 'block';\s*\}/g,
    `document.getElementById('nav-purchases').style.display = 'block';
    document.getElementById('nav-contacts').style.display = 'block';
    document.getElementById('nav-settlements').style.display = 'block';
    document.getElementById('nav-vouchers').style.display = 'block';
  }`
);

fs.writeFileSync('app_final.js', appFinalJs, 'utf8');

console.log('Fixed warehouse keeper and transaction columns!');
