import { MasterCounts, MasterRecord } from './my-schedule-master.types';

const esc = (v: any) =>
  String(v ?? '—')
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');

/**
 * Build the printable HTML for the master schedule PDF.
 * Rendered to PDF by puppeteer in the export service (landscape A4).
 */
export function buildMasterSchedulePdfHtml(
  records: MasterRecord[],
  counts: MasterCounts,
  opts?: { userName?: string },
): string {
  const generatedOn = new Date().toLocaleString('en-GB');

  const rows = records
    .map(
      (r) => `<tr>
        <td>${esc(r.sno)}</td>
        <td class="mono">${esc(r.audit_code)}</td>
        <td>${esc(r.audit_type)}</td>
        <td>${esc(r.company)}</td>
        <td>${esc(r.standards)}</td>
        <td>${esc(r.accreditation)}</td>
        <td>${esc(r.stage)}</td>
        <td>${esc(r.mode)}</td>
        <td>${esc(r.coordinator)} / ${esc(r.group)}</td>
        <td>${esc(r.date)}</td>
        <td>${esc(r.time)}</td>
        <td>${esc(r.lead_auditor)}</td>
        <td>${esc(r.status)}</td>
      </tr>`,
    )
    .join('');

  return `<!doctype html><html><head><meta charset="utf-8"/>
  <style>
    * { box-sizing: border-box; }
    body { font-family: 'Segoe UI', Arial, sans-serif; color: #0f172a; padding: 18px; }
    h1 { font-size: 18px; text-align: center; margin: 0 0 2px; color: #4a0080; }
    .sub { text-align:center; font-size: 11px; color:#64748b; margin: 0 0 10px; }
    .counts { text-align: center; font-size: 12px; color: #334155; margin: 0 0 14px; font-weight: 700; }
    .counts span { margin: 0 10px; }
    table { width: 100%; border-collapse: collapse; font-size: 9px; }
    th, td { border: 1px solid #94a3b8; padding: 5px 6px; text-align: left; vertical-align: top; }
    th { background: #4a0080; color: #fff; text-transform: uppercase; font-size: 8px; letter-spacing: 0.4px; }
    .mono { font-family: 'Courier New', monospace; }
    tr:nth-child(even) td { background: #f8fafc; }
    .footer { margin-top: 12px; font-size: 9px; color: #64748b; text-align: right; }
  </style></head><body>
    <h1>My Audit Schedule — Master List</h1>
    ${opts?.userName ? `<div class="sub">${esc(opts.userName)}</div>` : ''}
    <div class="counts">
      <span>Initial: ${counts.initial}</span>
      <span>Surveillance: ${counts.surveillance}</span>
      <span>Re-Certification: ${counts.recertification}</span>
      <span>Total: ${counts.total}</span>
    </div>
    <table>
      <thead><tr>
        <th>S#</th><th>Audit Code</th><th>Type</th><th>Company</th><th>Standard</th>
        <th>Accred.</th><th>Stage</th><th>Mode</th><th>Coordinator / Group</th>
        <th>Date</th><th>Time</th><th>Lead Auditor</th><th>Status</th>
      </tr></thead>
      <tbody>${
        rows ||
        '<tr><td colspan="13" style="text-align:center;padding:20px;">No audits found.</td></tr>'
      }</tbody>
    </table>
    <div class="footer">Generated from QRS Certification System · ${generatedOn}</div>
  </body></html>`;
}
