"""Admin notification templates for website contact inquiries."""
from __future__ import annotations

from html import escape as html_escape

from src.domain.public.models import ContactInquiry

SUBJECT_LABELS = {
    "general": "General Inquiry",
    "support": "Technical Support",
    "sales": "Sales / Enterprise",
    "partnership": "Partnership",
    "feedback": "Feedback",
}


def contact_subject_label(subject: str) -> str:
    return SUBJECT_LABELS.get(subject, subject)


def render_contact_notification(row: ContactInquiry) -> tuple[str, str, str]:
    full_name = f"{row.first_name} {row.last_name}".strip()
    topic = contact_subject_label(row.subject)
    subject = f"[P2HS] {topic} — {full_name}"
    company = row.company or "Not provided"
    text = "\n".join(
        [
            "New P2HS website inquiry",
            "",
            f"Topic: {topic}",
            f"Name: {full_name}",
            f"Email: {row.email}",
            f"Company: {company}",
            "",
            "Message",
            row.message,
            "",
            f"Reply to {row.email}",
        ]
    )
    html = _html_card(
        eyebrow="New website inquiry",
        heading=f"{html_escape(full_name)} sent a message",
        badge=html_escape(topic),
        rows=(
            ("Name", html_escape(full_name)),
            ("Email", f'<a href="mailto:{html_escape(row.email)}" style="color:#00f5d4;text-decoration:none;">{html_escape(row.email)}</a>'),
            ("Company", html_escape(company)),
        ),
        body_label="Message",
        body_html=html_escape(row.message).replace("\r\n", "<br>").replace("\n", "<br>"),
        footer="Sent from the P2HS contact form. Reply to this email to reach the visitor directly.",
    )
    return subject, html, text


def _html_card(
    *,
    eyebrow: str,
    heading: str,
    badge: str,
    rows: tuple[tuple[str, str], ...],
    body_label: str,
    body_html: str,
    footer: str,
) -> str:
    detail_rows = "".join(_detail_row(label, value) for label, value in rows)
    return f"""<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{html_escape(heading)}</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;">{html_escape(eyebrow)}</p>
                    <h1 style="margin:0 0 16px 0;font-size:24px;line-height:1.3;color:#fafafa;">{heading}</h1>
                    <span style="display:inline-block;padding:6px 12px;border-radius:999px;border:1px solid rgba(0,245,212,0.3);background:rgba(0,245,212,0.1);font-size:12px;font-weight:600;color:#00f5d4;">{badge}</span>
                  </td>
                </tr>
                <tr>
                  <td style="padding:24px 32px;">{detail_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;">{html_escape(body_label)}</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;">{body_html}</div>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td style="padding:20px 4px 0 4px;font-size:12px;line-height:1.6;color:#71717a;">{html_escape(footer)}</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>"""


def _detail_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:110px;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>"""
