import os
import smtplib
from email.message import EmailMessage
from flask import current_app
from config_loader import load_config

config = load_config()
API_BASE_URL = f"{config.app_base}:{config.service_port}"

def send_user_created_email(email, link, name=""):
    """
    DEV  -> return link only
    PROD -> send real email
    """

    if config.dev_mode == 1:
        return link   # 👈 só devolve o link

    host = os.getenv("RFP_SMTP_HOST", "localhost")
    port = int(os.getenv("RFP_SMTP_PORT", 25))

    msg = EmailMessage()
    msg["Subject"] = "Your account has been created"
    msg["From"] = "noreply@rfp.local"
    msg["To"] = email

    msg.set_content(f"""
        Hello {name or email},
        
        Your account was created.
        
        Set your password here:
        {link}
        """)

    with smtplib.SMTP(host, port) as s:
        s.send_message(msg)

    return link

def send_completion_email(email, download_url, job_id):
    """
    DEV  -> return download link
    PROD -> send real email
    """

    if config.dev_mode == 1:
        return download_url   # 👈 só devolve o link no DEV

    host = os.getenv("RFP_SMTP_HOST", "localhost")
    port = int(os.getenv("RFP_SMTP_PORT", 25))

    msg = EmailMessage()
    msg["Subject"] = "Your RFP processing is complete"
    msg["From"] = "noreply@rfp.local"
    msg["To"] = email

    msg.set_content(f"""
Hello,

Your RFP Excel processing has finished successfully.

Download your file here:
{download_url}

Job ID: {job_id}
""")

    with smtplib.SMTP(host, port) as s:
        s.send_message(msg)

    return None