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

async def send_report_email(
    email: str,
    project_name: str,
    pdf_path: str
):
    logger.info(f"Sending project report email to {email} for project {project_name}")
    html = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
    </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%;">
              <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;">
                        <img src="{os.getenv('FRONTEND_URL', 'http://localhost:3000')}/logo.png" width="24" height="24" alt="Orbit Logo" style="display: block; border: 0;" />
                      </td>
                      <td style="padding-left: 10px; font-size: 22px; font-weight: 700; color: #ffffff; letter-spacing: -0.5px;">
                        Orbit
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr>
                <td style="background-color: #1a1a24; border-radius: 16px; border: 1px solid rgba(255,255,255,0.06); overflow: hidden;">
                  <div style="height: 4px; background: linear-gradient(90deg, #6366f1, #8b5cf6, #a78bfa);"></div>
                  <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                      <td style="padding: 40px 36px 32px;">
                        <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;">Project Report</span>
                            </td>
                          </tr>
                        </table>
                        <h1 style="margin: 0 0 12px; font-size: 24px; font-weight: 800; color: #ffffff; line-height: 1.3;">
                          Your Report is Ready
                        </h1>
                        <p style="margin: 0 0 24px; font-size: 15px; color: #a1a1aa; line-height: 1.6;">
                          The requested project intelligence report for <strong style="color: #e4e4e7;">{project_name}</strong> has been generated successfully and is attached to this email.
                        </p>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
              <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"Project Report: {project_name}",
        recipients=[email],
        body=html,
        subtype=MessageType.html,
        attachments=[pdf_path]
    )
    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
