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_invite_email(
    email: str,
    invite_link: str,
    org_name: str = "your organization",
    inviter_name: str = "A team member"
):
    logger.info(f"Sending invite email to {email} with link {invite_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;">
                        
                        <!-- Invite 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;">Team Invitation</span>
                            </td>
                          </tr>
                        </table>

                        <!-- Heading -->
                        <h1 style="margin: 0 0 8px; font-size: 26px; font-weight: 800; color: #ffffff; line-height: 1.3;">
                          You've been invited to join
                        </h1>
                        <h2 style="margin: 0 0 20px; font-size: 22px; font-weight: 700; color: #818cf8; line-height: 1.3;">
                          {org_name}
                        </h2>

                        <!-- Subtext -->
                        <p style="margin: 0 0 28px; font-size: 15px; color: #a1a1aa; line-height: 1.6;">
                          <strong style="color: #e4e4e7;">{inviter_name}</strong> has invited you to collaborate on <strong style="color: #e4e4e7;">{org_name}</strong>'s workspace on Orbit. Accept the invite to set up your account and start working together.
                        </p>

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

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

                        <!-- Details Section -->
                        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin-bottom: 20px;">
                          <tr>
                            <td style="padding: 12px 16px; background-color: rgba(255,255,255,0.03); border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">
                              <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
                                <tr>
                                  <td style="padding-bottom: 8px;">
                                    <span style="font-size: 10px; font-weight: 700; color: #71717a; text-transform: uppercase; letter-spacing: 1.5px;">Invited Email</span>
                                  </td>
                                </tr>
                                <tr>
                                  <td>
                                    <span style="font-size: 14px; color: #e4e4e7; font-weight: 500;">{email}</span>
                                  </td>
                                </tr>
                              </table>
                            </td>
                          </tr>
                        </table>

                        <!-- 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="{invite_link}" style="color: #818cf8; word-break: break-all; text-decoration: underline;">{invite_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 invitation will expire in 7 days.
                  </p>
                  <p style="margin: 0; font-size: 11px; color: #3f3f46;">
                    If you didn't expect this email, you can safely ignore it.
                  </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"You're invited to join {org_name} on Orbit",
        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")