import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os


class Email:
    def __init__(self):
        self.smtp_server = "smtp.gmail.com"
        self.smtp_port = 587

    def send(self, to_email, subject, body, from_email=None, from_password=None):
        from_email = from_email or os.getenv("EMAIL_FROM")
        from_password = from_password or os.getenv("EMAIL_PASSWORD")

        if not from_email or not from_password:
            return "Email credentials not configured. Set EMAIL_FROM and EMAIL_PASSWORD in .env"

        try:
            msg = MIMEMultipart()
            msg["From"] = from_email
            msg["To"] = to_email
            msg["Subject"] = subject
            msg.attach(MIMEText(body, "plain"))

            server = smtplib.SMTP(self.smtp_server, self.smtp_port)
            server.starttls()
            server.login(from_email, from_password)
            server.sendmail(from_email, to_email, msg.as_string())
            server.quit()
            return f"Email sent to {to_email}"
        except Exception as e:
            return f"Failed to send email: {str(e)}"

    def send_quick(self, to_email, message):
        subject = "Message from Ana"
        return self.send(to_email, subject, message)
