const fs = require('fs');
const file = 'public_html/js/admin-panel.js';
let content = fs.readFileSync(file, 'utf8');

const replacements = [
    {
        find: '<h5>${n.title}</h5>\n            <p>${n.message}</p>',
        replace: '<h5>${window.escapeHtml(n.title)}</h5>\n            <p>${window.escapeHtml(n.message)}</p>'
    },
    {
        find: '<h5>${o.client?.name || \\'عميل\\'} ${o.captain ? \\'← \\' + o.captain.name : \\'\\'}</h5>\n                    <p>${o.pickup?.address || \\'?\\'} → ${o.dropoff?.address || \\'?\\'}</p>',
        replace: '<h5>${window.escapeHtml(o.client?.name || \\'عميل\\')} ${o.captain ? \\'← \\' + window.escapeHtml(o.captain.name) : \\'\\'}</h5>\n                    <p>${window.escapeHtml(o.pickup?.address || \\'?\\')} → ${window.escapeHtml(o.dropoff?.address || \\'?\\')}</p>'
    },
    {
        find: '<td style="font-weight:700;">${o.client?.name || \\'—\\'}</td>\n            <td>${o.captain?.name || \\'<span style="color:#94a3b8;">—</span>\\'}</td>\n            <td style="max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:12px;">${o.pickup?.address || \\'?\\'}</td>\n            <td style="max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:12px;">${o.dropoff?.address || \\'?\\'}</td>',
        replace: '<td style="font-weight:700;">${window.escapeHtml(o.client?.name || \\'—\\')}</td>\n            <td>${o.captain ? window.escapeHtml(o.captain.name) : \\'<span style="color:#94a3b8;">—</span>\\'}</td>\n            <td style="max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:12px;">${window.escapeHtml(o.pickup?.address || \\'?\\')}</td>\n            <td style="max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:12px;">${window.escapeHtml(o.dropoff?.address || \\'?\\')}</td>'
    },
    {
        find: '<td style="font-weight:700;">${u.name || \\'—\\'}</td>\n            <td dir="ltr" style="font-size:13px;">${u.phone || \\'—\\'}</td>',
        replace: '<td style="font-weight:700;">${window.escapeHtml(u.name || \\'—\\')}</td>\n            <td dir="ltr" style="font-size:13px;">${window.escapeHtml(u.phone || \\'—\\')}</td>'
    },
    {
        find: '<td style="font-weight:700;">${c.name || \\'—\\'}</td>\n            <td dir="ltr" style="font-size:13px;">${formattedPhone}</td>',
        replace: '<td style="font-weight:700;">${window.escapeHtml(c.name || \\'—\\')}</td>\n            <td dir="ltr" style="font-size:13px;">${window.escapeHtml(formattedPhone)}</td>'
    },
    {
        find: '<div style="width:44px;height:44px;border-radius:12px;background:linear-gradient(135deg,var(--gv-primary),var(--gv-accent));color:white;display:flex;align-items:center;justify-content:center;font-weight:800;font-size:16px;flex-shrink:0;">\n                ${(c.name || \\'?\\')[0]}\n            </div>\n            <div style="flex:1;min-width:140px;">\n                <div style="font-weight:700;font-size:14px;">${c.name}</div>\n                <div style="font-size:12px;color:var(--gv-dark-2);" dir="ltr">${formattedPhone} ${c.email ? \\'| \\' + c.email : \\'\\'}</div>',
        replace: '<div style="width:44px;height:44px;border-radius:12px;background:linear-gradient(135deg,var(--gv-primary),var(--gv-accent));color:white;display:flex;align-items:center;justify-content:center;font-weight:800;font-size:16px;flex-shrink:0;">\n                ${window.escapeHtml((c.name || \\'?\\')[0])}\n            </div>\n            <div style="flex:1;min-width:140px;">\n                <div style="font-weight:700;font-size:14px;">${window.escapeHtml(c.name || \\'—\\')}</div>\n                <div style="font-size:12px;color:var(--gv-dark-2);" dir="ltr">${window.escapeHtml(formattedPhone)} ${c.email ? \\'| \\' + window.escapeHtml(c.email) : \\'\\'}</div>'
    }
];

let replacedCount = 0;
for (const r of replacements) {
    if (content.includes(r.find)) {
        content = content.replace(r.find, r.replace);
        replacedCount++;
    } else {
        console.log("NOT FOUND:", r.find);
    }
}

fs.writeFileSync(file, content);
console.log(`Replaced ${replacedCount} out of ${replacements.length} sections`);
