const fs = require('fs');

// 1. Fix app_final.js (refreshContactStatement)
let appFinal = fs.readFileSync('app_final.js', 'utf8');

appFinal = appFinal.replace(
    /const advance = parseFloat\(ab\.advance_payment\) \|\| 0;/g, 
    "const advance = parseFloat(ab.advance_payment) || 0;\n                 const otherDed = parseFloat(ab.other_deductions) || 0;"
);

appFinal = appFinal.replace(
    /if\(advance > 0\) records\.push\(\{ date: ab\.date, type: 'استنزاف دفعة مقدمة', ref: '#' \+ ab\.id, notes: 'خصم دفعات مقدمة من المستخلص', debit: 0, credit: advance \}\);/g,
    "if(advance > 0) records.push({ date: ab.date, type: 'استنزاف دفعة مقدمة', ref: '#' + ab.id, notes: 'خصم دفعات مقدمة من المستخلص', debit: 0, credit: advance });\n                 if(otherDed > 0) records.push({ date: ab.date, type: 'استقطاعات أخرى', ref: '#' + ab.id, notes: 'استقطاعات متنوعة', debit: 0, credit: otherDed });"
);

appFinal = appFinal.replace(
    /if\(advance > 0\) records\.push\(\{ date: ab\.date, type: 'خصم دفعة مقدمة', ref: '#' \+ ab\.id, notes: 'دفعة مقدمة مخصومة', debit: advance, credit: 0 \}\);/g,
    "if(advance > 0) records.push({ date: ab.date, type: 'خصم دفعة مقدمة', ref: '#' + ab.id, notes: 'دفعة مقدمة مخصومة', debit: advance, credit: 0 });\n                 if(otherDed > 0) records.push({ date: ab.date, type: 'استقطاعات أخرى', ref: '#' + ab.id, notes: 'استقطاعات متنوعة', debit: otherDed, credit: 0 });"
);

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

// 2. Fix contractors_module.js (generateClientAbstractJournal)
let contMod = fs.readFileSync('contractors_module.js', 'utf8');

contMod = contMod.replace(
    /const advance = parseFloat\(ab\.advance_payment\) \|\| 0;/g,
    "const advance = parseFloat(ab.advance_payment) || 0;\n    const otherDed = parseFloat(ab.other_deductions) || 0;"
);

const clientOtherDedCode = `
    // Debit Other Deductions
    if(otherDed > 0) {
        const otherAcc = await window.resolveSystemAccount('استقطاعات مستخلصات العملاء', ['مصروفات'], 'expenses');
        rows.push({ account_id: otherAcc.id, debit: otherDed, credit: 0, project_id: ab.project_id });
    }`;

contMod = contMod.replace(
    /(\/\/ Credit Revenue\s*if\(gross > 0\))/g,
    clientOtherDedCode + '\n\n    $1'
);

fs.writeFileSync('contractors_module.js', contMod, 'utf8');

// 3. Fix subcontracts.js (generateSubcontractAbstractJournal)
let subMod = fs.readFileSync('subcontracts.js', 'utf8');

subMod = subMod.replace(
    /const advance = parseFloat\(a\.advance_payment\) \|\| 0;/g,
    "const advance = parseFloat(a.advance_payment) || 0;\n    const otherDed = parseFloat(a.other_deductions) || 0;"
);

const subOtherDedCode = `
    // Credit Other Deductions
    if(otherDed > 0) {
        const otherAcc = await window.resolveSystemAccount('استقطاعات متنوعة من مقاولي الباطن', ['إيرادات', 'أخرى'], 'revenues');
        rows.push({ account_id: otherAcc.id, debit: 0, credit: otherDed, project_id: cont.project_id });
    }`;

subMod = subMod.replace(
    /(\/\/ Credit Advance\s*if\(advance > 0\) \{[^}]+\})/g,
    '$1\n' + subOtherDedCode
);

fs.writeFileSync('subcontracts.js', subMod, 'utf8');

let indexHtml = fs.readFileSync('index.html', 'utf8');
indexHtml = indexHtml.replace(/v=20260610q/g, 'v=20260610r');
fs.writeFileSync('index.html', indexHtml, 'utf8');

console.log('Fixed missing other deductions!');
