from fastapi_mail import FastMail, MessageSchema
from fastapi_mail import MessageType
from utils.email_conf import conf
from utils.logging import logger

async def send_reset_password_email(
    email: str,
    reset_link: str,
    user_name: str = "User"
):
    logger.info(f"Sending reset password email to {email} with link {reset_link}")
    html = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body style="margin: 0; padding: 0; background-color: #0f0f13; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;">
      <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background-color: #0f0f13; padding: 40px 0;">
        <tr>
          <td align="center">
            <table role="presentation" width="520" cellpadding="0" cellspacing="0" style="max-width: 520px; width: 100%;">

              <!-- Logo Header -->
              <tr>
                <td align="center" style="padding-bottom: 32px;">
                  <table role="presentation" cellpadding="0" cellspacing="0">
                    <tr>
                      <td style="background: linear-gradient(135deg, #6366f1, #8b5cf6); padding: 10px 12px; border-radius: 10px; line-height: 1;">
                        <span style="font-size: 20px;">🚀</span>
                      </td>
                      <td style="padding-left: 10px; font-size: 22px; font-weight: 700; color: #ffffff; letter-spacing: -0.5px;">
                        Orbit
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>

              <!-- Main Card -->
              <tr>
                <td style="background-color: #1a1a24; border-radius: 16px; border: 1px solid rgba(255,255,255,0.06); overflow: hidden;">
                  
                  <!-- Gradient Accent Bar -->
                  <div style="height: 4px; background: linear-gradient(90deg, #6366f1, #8b5cf6, #a78bfa);"></div>

                  <!-- Content -->
                  <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                      <td style="padding: 40px 36px 32px;">
                        
                        <!-- Badge -->
                        <table role="presentation" cellpadding="0" cellspacing="0" style="margin-bottom: 24px;">
                          <tr>
                            <td style="background-color: rgba(99, 102, 241, 0.12); border: 1px solid rgba(99, 102, 241, 0.2); border-radius: 20px; padding: 6px 16px;">
                              <span style="font-size: 11px; font-weight: 700; color: #818cf8; text-transform: uppercase; letter-spacing: 1.5px;">Password Reset</span>
                            </td>
                          </tr>
                        </table>

                        <!-- Heading -->
                        <h1 style="margin: 0 0 16px; font-size: 26px; font-weight: 800; color: #ffffff; line-height: 1.3;">
                          Reset your password
                        </h1>

                        <!-- Subtext -->
                        <p style="margin: 0 0 28px; font-size: 15px; color: #a1a1aa; line-height: 1.6;">
                          Hi <strong style="color: #e4e4e7;">{user_name}</strong>,<br/><br/>
                          We received a request to reset the password for your Orbit account associated with <strong style="color: #e4e4e7;">{email}</strong>. 
                          Click the button below to set a new password.
                        </p>

                        <!-- CTA Button -->
                        <table role="presentation" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom: 28px;">
                          <tr>
                            <td align="center">
                              <a href="{reset_link}" target="_blank" style="display: inline-block; background: linear-gradient(135deg, #6366f1, #7c3aed); color: #ffffff; text-decoration: none; font-size: 15px; font-weight: 700; padding: 14px 40px; border-radius: 10px; letter-spacing: 0.3px; box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);">
                                Reset Password →
                              </a>
                            </td>
                          </tr>
                        </table>

                        <!-- Divider -->
                        <div style="height: 1px; background-color: rgba(255,255,255,0.06); margin: 24px 0;"></div>

                        <!-- Fallback Link -->
                        <p style="margin: 0; font-size: 12px; color: #52525b; line-height: 1.6;">
                          If the button above doesn't work, copy and paste this link into your browser:<br />
                          <a href="{reset_link}" style="color: #818cf8; word-break: break-all; text-decoration: underline;">{reset_link}</a>
                        </p>

                      </td>
                    </tr>
                  </table>
                </td>
              </tr>

              <!-- Footer -->
              <tr>
                <td align="center" style="padding: 28px 16px 0;">
                  <p style="margin: 0 0 4px; font-size: 11px; color: #3f3f46;">
                    This password reset link will expire in 15 minutes.
                  </p>
                  <p style="margin: 0; font-size: 11px; color: #3f3f46;">
                    If you didn't request a password reset, you can safely ignore this email.
                  </p>
                  <p style="margin: 16px 0 0; font-size: 10px; color: #27272a;">
                    © Orbit — Enterprise Project Management
                  </p>
                </td>
              </tr>

            </table>
          </td>
        </tr>
      </table>
    </body>
    </html>
    """
    logger.debug(f"Email content prepared for {email}")
    message = MessageSchema(
        subject=f"Reset your Orbit password",
        recipients=[email],
        body=html,
        subtype=MessageType.html
    )
    logger.info(f"Email message created for {email}")
    fm = FastMail(conf)
    try:
        logger.info("[INFO] Sending SMTP email...")
        await fm.send_message(message)
        logger.info("[SUCCESS] Email sent successfully")
        return True
    except Exception as e:
        logger.exception("[ERROR] SMTP FAILED")
        return False
