const fs = require('fs');

let html = fs.readFileSync('d:/برنامج مخازن/index.html', 'utf8');

// Replace sales discount input
html = html.replace(
    /<div class="flex-col"><label>الخصم \(قيمة\)<\/label><input type="number" id="sales-discount" min="0" value="0"><\/div>/g,
    `<div class="flex-col">
      <label>الخصم</label>
      <div style="display:flex; gap:5px;">
        <input type="number" id="sales-discount" min="0" value="0" style="flex:1; min-width: 50px;">
        <select id="sales-discount-type" style="width:75px; padding: 5px;">
          <option value="percent">نسبة %</option>
          <option value="value">قيمة</option>
        </select>
      </div>
    </div>`
);

// Replace purchases discount input
html = html.replace(
    /<div class="flex-col"><label>الخصم \(قيمة\)<\/label><input type="number" id="purchases-discount" min="0" value="0"><\/div>/g,
    `<div class="flex-col">
      <label>الخصم</label>
      <div style="display:flex; gap:5px;">
        <input type="number" id="purchases-discount" min="0" value="0" style="flex:1; min-width: 50px;">
        <select id="purchases-discount-type" style="width:75px; padding: 5px;">
          <option value="percent">نسبة %</option>
          <option value="value">قيمة</option>
        </select>
      </div>
    </div>`
);

// Bust cache
html = html.replace(/src="sales_purchases_module\.js\?v=[^"]*"/g, `src="sales_purchases_module.js?v=${Date.now()}"`);
fs.writeFileSync('d:/برنامج مخازن/index.html', html);


let js = fs.readFileSync('d:/برنامج مخازن/sales_purchases_module.js', 'utf8');

// Update addSalesLine
js = js.replace(
    /const discount = parseFloat\(document\.getElementById\('sales-discount'\)\.value\) \|\| 0;\s*currentSalesLines\.push\(\{ item_id: itemId, qty, price, discount \}\);/g,
    `const discount = parseFloat(document.getElementById('sales-discount').value) || 0;
    const discountTypeEl = document.getElementById('sales-discount-type');
    const discount_type = discountTypeEl ? discountTypeEl.value : 'percent';
    currentSalesLines.push({ item_id: itemId, qty, price, discount, discount_type });`
);

// Update renderSalesLines
js = js.replace(
    /const lineDiscountValue = lineTotalBeforeDiscount \* \(line\.discount \/ 100\);/g,
    `let lineDiscountValue = 0;
    const isPercent = line.discount_type === 'percent' || !line.discount_type;
    if (isPercent) {
        lineDiscountValue = lineTotalBeforeDiscount * (line.discount / 100);
    } else {
        lineDiscountValue = line.discount || 0;
    }
    if (lineDiscountValue > lineTotalBeforeDiscount) lineDiscountValue = lineTotalBeforeDiscount;`
);

js = js.replace(
    /<td>\$\{line\.discount\}%<\/td>/g,
    `<td>\${line.discount} \${(line.discount_type === 'percent' || !line.discount_type) ? '%' : 'قيمة'}</td>`
);

// Update addPurchasesLine
js = js.replace(
    /const discount = parseFloat\(document\.getElementById\('purchases-discount'\)\.value\) \|\| 0;\s*currentPurchasesLines\.push\(\{ item_id: itemId, qty, price, discount \}\);/g,
    `const discount = parseFloat(document.getElementById('purchases-discount').value) || 0;
    const discountTypeEl = document.getElementById('purchases-discount-type');
    const discount_type = discountTypeEl ? discountTypeEl.value : 'percent';
    currentPurchasesLines.push({ item_id: itemId, qty, price, discount, discount_type });`
);

// Update renderPurchasesLines
js = js.replace(
    /const lineDiscountValue = lineTotalBeforeDiscount \* \(line\.discount \/ 100\);/g,
    `let lineDiscountValue = 0;
    const isPercent = line.discount_type === 'percent' || !line.discount_type;
    if (isPercent) {
        lineDiscountValue = lineTotalBeforeDiscount * (line.discount / 100);
    } else {
        lineDiscountValue = line.discount || 0;
    }
    if (lineDiscountValue > lineTotalBeforeDiscount) lineDiscountValue = lineTotalBeforeDiscount;`
);

js = js.replace(
    /<td>\$\{line\.discount\}%<\/td>/g,
    `<td>\${line.discount} \${(line.discount_type === 'percent' || !line.discount_type) ? '%' : 'قيمة'}</td>`
);

fs.writeFileSync('d:/برنامج مخازن/sales_purchases_module.js', js);
console.log("Successfully updated discount types and busted cache!");
