import sys

with open('public_html/js/admin-panel.js', 'r', encoding='utf-8') as f:
    content = f.read()

replacements = [
    (
        "<h5>${n.title}</h5>\n            <p>${n.message}</p>",
        "<h5>${window.escapeHtml(n.title)}</h5>\n            <p>${window.escapeHtml(n.message)}</p>"
    ),
    (
        "<h5>${o.client?.name || 'عميل'} ${o.captain ? '← ' + o.captain.name : ''}</h5>\n                    <p>${o.pickup?.address || '?'} → ${o.dropoff?.address || '?'}</p>",
        "<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>"
    ),
    (
        "<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>",
        "<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>"
    ),
    (
        "<td style=\"font-weight:700;\">${u.name || '—'}</td>\n            <td dir=\"ltr\" style=\"font-size:13px;\">${u.phone || '—'}</td>",
        "<td style=\"font-weight:700;\">${window.escapeHtml(u.name || '—')}</td>\n            <td dir=\"ltr\" style=\"font-size:13px;\">${window.escapeHtml(u.phone || '—')}</td>"
    ),
    (
        "<td style=\"font-weight:700;\">${c.name || '—'}</td>\n            <td dir=\"ltr\" style=\"font-size:13px;\">${formattedPhone}</td>",
        "<td style=\"font-weight:700;\">${window.escapeHtml(c.name || '—')}</td>\n            <td dir=\"ltr\" style=\"font-size:13px;\">${window.escapeHtml(formattedPhone)}</td>"
    ),
    (
        "<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>",
        "<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>"
    )
]

count = 0
for fnd, rpl in replacements:
    if fnd in content:
        content = content.replace(fnd, rpl)
        count += 1
    else:
        print("NOT FOUND:", repr(fnd))

with open('public_html/js/admin-panel.js', 'w', encoding='utf-8') as f:
    f.write(content)

print(f"Replaced {count} occurrences")
