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

FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:3000")

async def send_mention_email(
    email: str,
    mentioned_by_name: str,
    ticket_name: str,
    ticket_number: str,
    comment_text: str,
    ticket_link: str
):
    logger.info(f"Sending mention email to {email} for ticket {ticket_number}")
    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;">
                        
                        <!-- Mention 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;">New Mention</span>
                            </td>
                          </tr>
                        </table>

                        <!-- Heading -->
                        <h1 style="margin: 0 0 12px; font-size: 24px; font-weight: 800; color: #ffffff; line-height: 1.3;">
                          You were mentioned in a comment
                        </h1>

                        <!-- Subtext -->
                        <p style="margin: 0 0 24px; font-size: 15px; color: #a1a1aa; line-height: 1.6;">
                          <strong style="color: #e4e4e7;">{mentioned_by_name}</strong> tagged you in the ticket <strong style="color: #e4e4e7;">{ticket_number}: {ticket_name}</strong>.
                        </p>

                        <!-- Comment Box -->
                        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin-bottom: 28px;">
                          <tr>
                            <td style="padding: 20px; background-color: rgba(255,255,255,0.03); border-radius: 12px; border: 1px solid rgba(255,255,255,0.05);">
                              <p style="margin: 0; font-size: 14px; color: #e4e4e7; line-height: 1.6; font-style: italic;">
                                "{comment_text}"
                              </p>
                            </td>
                          </tr>
                        </table>

                        <!-- CTA Button -->
                        <table role="presentation" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom: 12px;">
                          <tr>
                            <td align="center">
                              <a href="{ticket_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);">
                                View Comment →
                              </a>
                            </td>
                          </tr>
                        </table>

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

              <!-- Footer -->
              <tr>
                <td align="center" style="padding: 28px 16px 0;">
                  <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"{mentioned_by_name} mentioned you in {ticket_number}",
        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
