"""HTML + plain-text email bodies for Pole Power Academy."""

from html import escape

BRAND_NAME = "Pole Power Academy"
BRAND_PURPLE = "#682F93"
BRAND_BG = "#f8f5fc"
BRAND_TEXT = "#334155"
BRAND_MUTED = "#64748b"


def _base_html(*, title: str, body_html: str) -> str:
    safe_title = escape(title)
    return f"""<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>{safe_title}</title>
</head>
<body style="margin:0;padding:0;background-color:{BRAND_BG};font-family:'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif;">
  <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color:{BRAND_BG};padding:32px 16px;">
    <tr>
      <td align="center">
        <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="max-width:560px;">
          <tr>
            <td align="center" style="padding-bottom:24px;">
              <p style="margin:0;font-family:Georgia,'Times New Roman',serif;font-size:28px;font-weight:700;color:{BRAND_PURPLE};letter-spacing:0.3px;">
                {escape(BRAND_NAME)}
              </p>
            </td>
          </tr>
          <tr>
            <td style="background-color:#ffffff;border-radius:16px;box-shadow:0 4px 24px rgba(104,47,147,0.08);overflow:hidden;">
              <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
                <tr>
                  <td style="height:6px;background-color:{BRAND_PURPLE};font-size:0;line-height:0;">&nbsp;</td>
                </tr>
                <tr>
                  <td style="padding:36px 32px 32px;">
                    {body_html}
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td align="center" style="padding-top:24px;">
              <p style="margin:0;font-size:12px;line-height:1.6;color:{BRAND_MUTED};">
                &copy; {escape(BRAND_NAME)}. Strength. Confidence. Fun.
              </p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>"""


def _otp_display_html(otp: str) -> str:
    safe_otp = escape(otp)
    digits = "".join(
        f'<span style="display:inline-block;min-width:44px;padding:23px 8px;margin:0 4px;'
        f'font-size:28px;font-weight:700;letter-spacing:4px;color:{BRAND_PURPLE};'
        f'background-color:{BRAND_BG};border-radius:10px;border:1px solid #e9d5f5;">{ch}</span>'
        for ch in safe_otp
    )
    return f"""
    <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td align="center" style="padding:8px 0 24px;">
          <div style="font-size:0;line-height:0;">{digits}</div>
        </td>
      </tr>
    </table>"""


def render_verification_otp_html(*, otp: str, expiry_minutes: int = 15) -> str:
    body = f"""
    <h1 style="margin:0 0 12px;font-size:22px;font-weight:700;color:{BRAND_TEXT};">Verify your email</h1>
    <p style="margin:0 0 20px;font-size:15px;line-height:1.6;color:{BRAND_MUTED};">
      Use the verification code below to complete your registration at {escape(BRAND_NAME)}.
    </p>
    {_otp_display_html(otp)}
    <p style="margin:0 0 8px;font-size:14px;line-height:1.6;color:{BRAND_TEXT};text-align:center;">
      <strong>Your code:</strong> <span style="color:{BRAND_PURPLE};font-size:18px;letter-spacing:2px;">{escape(otp)}</span>
    </p>
    <p style="margin:16px 0 0;font-size:13px;line-height:1.6;color:{BRAND_MUTED};text-align:center;">
      This code expires in <strong>{expiry_minutes} minutes</strong>.
    </p>
    <p style="margin:24px 0 0;font-size:13px;line-height:1.6;color:{BRAND_MUTED};text-align:center;">
      If you did not create an account, you can safely ignore this email.
    </p>
  """
    return _base_html(title="Email verification", body_html=body)


def render_password_reset_otp_html(*, otp: str, expiry_minutes: int = 15) -> str:
    body = f"""
    <h1 style="margin:0 0 12px;font-size:22px;font-weight:700;color:{BRAND_TEXT};">Reset your password</h1>
    <p style="margin:0 0 20px;font-size:15px;line-height:1.6;color:{BRAND_MUTED};">
      We received a request to reset your {escape(BRAND_NAME)} password. Enter this code on the reset password screen.
    </p>
    {_otp_display_html(otp)}
    <p style="margin:0 0 8px;font-size:14px;line-height:1.6;color:{BRAND_TEXT};text-align:center;">
      <strong>Your code:</strong> <span style="color:{BRAND_PURPLE};font-size:18px;letter-spacing:2px;">{escape(otp)}</span>
    </p>
    <p style="margin:16px 0 0;font-size:13px;line-height:1.6;color:{BRAND_MUTED};text-align:center;">
      This code expires in <strong>{expiry_minutes} minutes</strong>.
    </p>
    <p style="margin:24px 0 0;font-size:13px;line-height:1.6;color:{BRAND_MUTED};text-align:center;">
      If you did not request a password reset, you can safely ignore this email.
    </p>
  """
    return _base_html(title="Password reset", body_html=body)


def render_verification_otp_plain(*, otp: str, expiry_minutes: int = 15) -> str:
    return (
        f"{BRAND_NAME} — Email verification\n\n"
        f"Your verification code is: {otp}\n\n"
        f"This code expires in {expiry_minutes} minutes.\n"
        f"If you did not create an account, you can ignore this email.\n\n"
        f"— {BRAND_NAME}"
    )


def render_password_reset_otp_plain(*, otp: str, expiry_minutes: int = 15) -> str:
    return (
        f"{BRAND_NAME} — Password reset\n\n"
        f"Your password reset code is: {otp}\n\n"
        f"Enter this code on the reset password screen. "
        f"It expires in {expiry_minutes} minutes.\n\n"
        f"If you did not request a password reset, you can ignore this email.\n\n"
        f"— {BRAND_NAME}"
    )
