import type { RecoResult, RecommendedModule } from "./recommendations";

function euros(n: number): string {
  return `${n.toLocaleString("fr-FR")} €`;
}

function moduleListHtml(modules: RecommendedModule[]): string {
  if (modules.length === 0) return "<p style='color:#94a3b8;'>Aucun module pour l'instant.</p>";
  return `
    <ul style="margin:0;padding-left:20px;color:#334155;">
      ${modules
        .map((m) => `<li style="margin-bottom:4px;">${m.label} — <strong>${euros(m.price)}</strong></li>`)
        .join("")}
    </ul>
  `;
}

const WRAPPER = `font-family: -apple-system, Segoe UI, Roboto, Arial, sans-serif; max-width:600px; margin:0 auto; background:#ffffff; color:#0f172a;`;
const HEADER = `background: linear-gradient(135deg, #0B1B2B 0%, #071322 100%); padding:24px 32px; color:#fff;`;
const SECTION = `padding:24px 32px; border-bottom:1px solid #e2e8f0;`;

export function buildLeadConfirmationEmail(data: {
  name: string;
  reco: RecoResult;
}): { subject: string; html: string } {
  const { name, reco } = data;
  const firstName = name.split(" ")[0];
  const subject = `${firstName}, votre projet de plateforme est prêt`;

  const html = `
    <div style="${WRAPPER}">
      <div style="${HEADER}">
        <h1 style="margin:0;font-size:20px;">AZELIOR</h1>
        <p style="margin:8px 0 0;color:#cbd5e1;">Votre étude personnalisée</p>
      </div>
      <div style="${SECTION}">
        <p>Bonjour ${firstName},</p>
        <p>Voici la première version de l'architecture que nous vous recommandons pour votre projet.</p>
      </div>
      <div style="${SECTION}">
        <h2 style="font-size:16px;color:#0B1B2B;">Modules recommandés</h2>
        ${moduleListHtml(reco.recommended)}
      </div>
      ${
        reco.reserve.length > 0
          ? `<div style="${SECTION}">
              <h2 style="font-size:16px;color:#0B1B2B;">Modules en réserve (prochaine étape)</h2>
              ${moduleListHtml(reco.reserve)}
            </div>`
          : ""
      }
      <div style="${SECTION}">
        <table style="width:100%;text-align:center;border-collapse:collapse;">
          <tr>
            <td style="padding:8px;">
              <div style="font-size:22px;font-weight:bold;color:#22C55E;">${reco.hoursSavedPerWeek}h</div>
              <div style="font-size:12px;color:#64748b;">économisées / semaine</div>
            </td>
            <td style="padding:8px;">
              <div style="font-size:22px;font-weight:bold;color:#38BDF8;">${reco.automationPotential}%</div>
              <div style="font-size:12px;color:#64748b;">automatisation</div>
            </td>
            <td style="padding:8px;">
              <div style="font-size:22px;font-weight:bold;color:#0B1B2B;">${reco.timeWeeksMin}-${reco.timeWeeksMax} sem.</div>
              <div style="font-size:12px;color:#64748b;">délai estimé</div>
            </td>
          </tr>
        </table>
      </div>
      <div style="padding:24px 32px;">
        <p>Un conseiller Azelior vous recontacte sous 24 à 48h avec votre étude complète, votre devis détaillé et votre feuille de route.</p>
        <p style="color:#64748b;font-size:12px;margin-top:24px;">Azelior — Plateformes métiers sur-mesure</p>
      </div>
    </div>
  `;

  return { subject, html };
}

export function buildInternalLeadNotificationEmail(data: {
  name: string;
  email: string;
  phone?: string;
  company?: string;
  sector?: string;
  project?: string;
  budget: number;
  reco: RecoResult;
}): { subject: string; html: string } {
  const { name, email, phone, company, sector, project, budget, reco } = data;
  const subject = `Nouveau lead : ${name}${company ? ` (${company})` : ""} — ${euros(budget)}`;

  const html = `
    <div style="${WRAPPER}">
      <div style="${SECTION}">
        <h2 style="margin-top:0;">Nouveau lead configurateur</h2>
        <p><strong>Nom :</strong> ${name}</p>
        <p><strong>Email :</strong> ${email}</p>
        ${phone ? `<p><strong>Téléphone :</strong> ${phone}</p>` : ""}
        ${company ? `<p><strong>Entreprise :</strong> ${company}</p>` : ""}
        ${sector ? `<p><strong>Secteur :</strong> ${sector}</p>` : ""}
        ${project ? `<p><strong>Projet décrit :</strong> ${project}</p>` : ""}
        <p><strong>Budget :</strong> ${euros(budget)}</p>
        <p><strong>Complexité estimée :</strong> ${reco.complexity}</p>
      </div>
      <div style="${SECTION}">
        <h3 style="font-size:14px;">Modules recommandés</h3>
        ${moduleListHtml(reco.recommended)}
      </div>
    </div>
  `;

  return { subject, html };
}
