const fs = require('fs');
let c = fs.readFileSync('index.html', 'utf8');

const navTarget = '<li data-view="audit-logs" id="nav-audit-logs"';
const navInject = '<li data-view="settings" id="nav-settings" style="display:none; color: #f1c40f; font-weight: bold;">الإعدادات العامة</li>\n        ';
if (!c.includes('id="nav-settings"')) {
    c = c.replace(navTarget, navInject + navTarget);
}

const viewTarget = '<div id="view-audit-logs" class="view">';
const viewInject = `      <div id="view-settings" class="view">
        <div class="header-actions">
          <h2>الإعدادات العامة للشركة</h2>
          <button class="btn-primary" id="btn-save-settings" onclick="saveSettings()">💾 حفظ الإعدادات</button>
        </div>
        
        <div class="form-grid" style="margin-top: 20px;">
          <div class="form-group">
            <label>اسم الشركة / المؤسسة</label>
            <input type="text" id="settings-company-name" placeholder="أدخل اسم الشركة...">
          </div>
          <div class="form-group">
            <label>الرقم الضريبي</label>
            <input type="text" id="settings-tax-number" placeholder="أدخل الرقم الضريبي...">
          </div>
          <div class="form-group">
            <label>نسبة ضريبة القيمة المضافة (%)</label>
            <input type="number" id="settings-vat-rate" placeholder="مثال: 15" value="15">
          </div>
          <div class="form-group" style="grid-column: span 2;">
            <label>شعار الشركة (اللوجو)</label>
            <input type="file" id="settings-logo-upload" accept="image/*" onchange="handleLogoUpload(event)">
            <img id="settings-logo-preview" src="" alt="شعار الشركة" style="max-width: 200px; max-height: 150px; display: none; margin-top: 10px; border-radius: 8px; border: 1px solid #ccc; padding: 5px; background: white;">
          </div>
        </div>
      </div>\n\n      `;

if (!c.includes('id="view-settings"')) {
    c = c.replace(viewTarget, viewInject + viewTarget);
}

const scriptTarget = '<script src="app_final.js?v=';
const scriptInject = '<script src="settings_module.js?v=1"></script>\n  ';
if (!c.includes('settings_module.js')) {
    c = c.replace(scriptTarget, scriptInject + scriptTarget);
}

fs.writeFileSync('index.html', c);
console.log('Patched index.html with settings view');
