from django.core.mail import send_mail
from django.conf import settings

LOGO_URL = f"{settings.SITE_URL.rstrip('/')}/static/images/logo.png"


# ------------------------------------------------------------------ #
# REGISTRATION OTP EMAIL
# ------------------------------------------------------------------ #
def send_otp_email(email, otp, org_name):
    subject = 'Your Avighna Solution Verification Code'

    message = f"""
Hello,

Thank you for registering {org_name} on Avighna Solution.

Your verification code is: {otp}

This code is valid for 10 minutes.
Do not share this code with anyone.

Regards,
Avighna Solution Team
support@avighnasolution.in
    """.strip()

    html_message = f"""
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    body {{ font-family:'Segoe UI',Arial,sans-serif; background:#f4f6f9; margin:0; padding:0; }}
    .wrapper {{ max-width:520px; margin:40px auto; background:white; border-radius:16px; overflow:hidden; box-shadow:0 4px 20px rgba(0,0,0,.1); }}
    .header  {{ background:#1a3c5e; padding:28px 32px; text-align:center; }}
    .header h1 {{ color:white; font-size:1.3rem; margin:0; }}
    .body    {{ padding:32px; text-align:center; }}
    .body p  {{ color:#555; font-size:.95rem; line-height:1.7; margin:0 0 20px; }}
    .otp-box {{ background:#f4f6f9; border:2px dashed #1a3c5e; border-radius:12px; padding:20px; margin:24px 0; }}
    .otp     {{ font-size:2.4rem; font-weight:700; color:#1a3c5e; letter-spacing:10px; }}
    .note    {{ font-size:.82rem; color:#999; margin-top:8px; }}
    .footer  {{ background:#f9fafb; padding:18px 32px; text-align:center; font-size:.8rem; color:#aaa; }}
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="header"><img src="{LOGO_URL}" alt="Avighna Solution" style="height:40px;margin-bottom:6px;"><h1>Avighna Solution</h1></div>
    <div class="body">
      <p>Hello,<br>Thank you for registering <strong>{org_name}</strong> on Avighna Solution.<br>Use the code below to verify your email address.</p>
      <div class="otp-box">
        <div class="otp">{otp}</div>
        <div class="note">Valid for 10 minutes only</div>
      </div>
      <p style="font-size:.85rem;color:#888;">Do not share this code with anyone.<br>If you did not request this, please ignore this email.</p>
    </div>
    <div class="footer">&copy; 2026 Avighna Solution &nbsp;|&nbsp; support@avighnasolution.in</div>
  </div>
</body>
</html>
    """.strip()

    try:
        send_mail(
            subject        = subject,
            message        = message,
            from_email     = settings.DEFAULT_FROM_EMAIL,
            recipient_list = [email],
            html_message   = html_message,
            fail_silently  = False,
        )
        return True
    except Exception as e:
        print(f"OTP email error: {e}")
        return False


# ------------------------------------------------------------------ #
# LOGIN OTP EMAIL (new device)
# ------------------------------------------------------------------ #
def send_login_otp_email(email, otp, name, device):
    subject = 'New Device Login — Verification Code'

    message = f"""
Hello {name},

A login attempt was made from a new device.
Device: {device}

Your OTP: {otp}

Valid for 10 minutes.

If this was not you, change your password immediately.

Regards,
Avighna Solution Team
    """.strip()

    html_message = f"""
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    body {{ font-family:'Segoe UI',Arial,sans-serif; background:#f4f6f9; margin:0; padding:0; }}
    .wrapper {{ max-width:520px; margin:40px auto; background:white; border-radius:16px; overflow:hidden; box-shadow:0 4px 20px rgba(0,0,0,.1); }}
    .header  {{ background:#1a3c5e; padding:28px 32px; text-align:center; }}
    .header h1 {{ color:white; font-size:1.3rem; margin:0; }}
    .body    {{ padding:32px; text-align:center; }}
    .body p  {{ color:#555; font-size:.95rem; line-height:1.7; margin:0 0 16px; }}
    .device-box {{ background:#fff8e1; border:1px solid #ffe082; border-radius:10px; padding:12px 16px; margin:16px 0; font-size:.88rem; color:#5d4037; }}
    .otp-box {{ background:#f4f6f9; border:2px dashed #1a3c5e; border-radius:12px; padding:20px; margin:20px 0; }}
    .otp     {{ font-size:2.4rem; font-weight:700; color:#1a3c5e; letter-spacing:10px; }}
    .note    {{ font-size:.82rem; color:#999; margin-top:8px; }}
    .footer  {{ background:#f9fafb; padding:18px 32px; text-align:center; font-size:.8rem; color:#aaa; }}
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="header"><img src="{LOGO_URL}" alt="Avighna Solution" style="height:40px;margin-bottom:6px;"><h1>Avighna Solution</h1></div>
    <div class="body">
      <p>Hello <strong>{name}</strong>,</p>
      <p>A login attempt was made from a new device.</p>
      <div class="device-box">📱 New device: <strong>{device}</strong></div>
      <p>Use the code below to verify:</p>
      <div class="otp-box">
        <div class="otp">{otp}</div>
        <div class="note">Valid for 10 minutes only</div>
      </div>
      <p style="font-size:.85rem;color:#d62828;">If this was not you, change your password immediately.</p>
    </div>
    <div class="footer">&copy; 2026 Avighna Solution &nbsp;|&nbsp; support@avighnasolution.in</div>
  </div>
</body>
</html>
    """.strip()

    try:
        send_mail(
            subject        = subject,
            message        = message,
            from_email     = settings.DEFAULT_FROM_EMAIL,
            recipient_list = [email],
            html_message   = html_message,
            fail_silently  = False,
        )
        return True
    except Exception as e:
        print(f"Login OTP email error: {e}")
        return False