"""Admin notification templates for enterprise / custom-pricing inquiries."""
from __future__ import annotations

from html import escape as html_escape

from src.domain.billing.models import EnterpriseInquiry


def render_enterprise_inquiry_notification(row: EnterpriseInquiry) -> tuple[str, str, str]:
    full_name = f"{row.first_name} {row.last_name}".strip()
    subject = f"[P2HS] Enterprise inquiry — {full_name}"
    extra = row.additional_features or "Not provided"
    agency = "Yes" if row.is_agency_account else "No"
    text = "\n".join(
        [
            "New P2HS enterprise inquiry",
            "",
            f"Name: {full_name}",
            f"Email: {row.email}",
            f"Phone: {row.phone}",
            f"Websites: {row.website_count}",
            f"AI credit minutes: {row.ai_credit_minutes_desired}",
            f"Agency account: {agency}",
            f"Employees: {row.employee_count}",
            f"Plan: {row.plan_slug or 'Not specified'}",
            f"Workspace: {row.workspace_id or 'None'}",
            "",
            "Additional features",
            extra,
            "",
            f"Reply to {row.email}",
        ]
    )
    message_html = html_escape(extra).replace("\r\n", "<br>").replace("\n", "<br>")
    rows = "".join(
        [
            _row("Name", html_escape(full_name)),
            _row("Email", f'<a href="mailto:{html_escape(row.email)}" style="color:#00f5d4;text-decoration:none;">{html_escape(row.email)}</a>'),
            _row("Phone", html_escape(row.phone)),
            _row("Websites", str(row.website_count)),
            _row("AI minutes", str(row.ai_credit_minutes_desired)),
            _row("Agency", agency),
            _row("Employees", str(row.employee_count)),
            _row("Plan", html_escape(row.plan_slug or "Not specified")),
        ]
    )
    html = f"""<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>{html_escape(subject)}</title></head>
<body style="margin:0;padding:0;background:#09090b;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#09090b;padding:32px 16px;">
    <tr><td align="center">
      <table role="presentation" width="600" cellpadding="0" cellspacing="0" style="width:100%;max-width:600px;font-family:Inter,Segoe UI,Helvetica,Arial,sans-serif;">
        <tr><td style="padding:0 0 20px 4px;"><span style="font-size:13px;letter-spacing:0.18em;font-weight:700;color:#00f5d4;">P2HS</span></td></tr>
        <tr><td style="background:#121214;border:1px solid rgba(255,255,255,0.08);border-radius:20px;overflow:hidden;">
          <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
            <tr><td style="height:4px;background:linear-gradient(90deg,#00f5d4,#7b2cbf);font-size:0;line-height:0;">&nbsp;</td></tr>
            <tr><td style="padding:28px 32px 8px 32px;">
              <p style="margin:0 0 8px 0;font-size:12px;font-weight:600;letter-spacing:0.14em;text-transform:uppercase;color:#00f5d4;">Enterprise inquiry</p>
              <h1 style="margin:0 0 16px 0;font-size:24px;line-height:1.3;color:#fafafa;">{html_escape(full_name)} requested custom pricing</h1>
            </td></tr>
            <tr><td style="padding:24px 32px;">{rows}</td></tr>
            <tr><td style="padding:0 32px 32px 32px;">
              <p style="margin:0 0 10px 0;font-size:12px;font-weight:600;letter-spacing:0.12em;text-transform:uppercase;color:#a1a1aa;">Additional features</p>
              <div style="padding:18px 20px;border-radius:14px;background:#1a1a1e;border:1px solid rgba(255,255,255,0.06);font-size:15px;line-height:1.7;color:#fafafa;">{message_html}</div>
            </td></tr>
          </table>
        </td></tr>
        <tr><td style="padding:20px 4px 0 4px;font-size:12px;line-height:1.6;color:#71717a;">Sent from the P2HS enterprise form. Reply to this email to reach the requester directly.</td></tr>
      </table>
    </td></tr>
  </table>
</body>
</html>"""
    return subject, html, text


def _row(label: str, value: str) -> str:
    return f"""<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:0 0 14px 0;">
    <tr>
      <td style="width:130px;padding:0 12px 0 0;vertical-align:top;font-size:12px;font-weight:600;letter-spacing:0.08em;text-transform:uppercase;color:#a1a1aa;">{html_escape(label)}</td>
      <td style="padding:0;font-size:15px;line-height:1.5;color:#fafafa;">{value}</td>
    </tr>
  </table>"""
