import smtplib
from email.mime.text import MIMEText
import logging

logger = logging.getLogger(__name__)

class EmailSender:
    def __init__(self, config):
        self.config = config
        self.sender_address = config.get('sender_address')
        self.recipient_address = config.get('recipient_address')
        self.smtp_server = config.get('smtp_server')
        self.smtp_port = config.get('smtp_port')
        self.smtp_username = config.get('smtp_username')
        self.smtp_password = config.get('smtp_password')

    def send_email(self, subject, body, details=None):
        if not self.config.get('enabled', False):
            logger.warning("Email alerts are disabled.")
            return

        try:
            msg = MIMEText(f"{body}\n\nDetails: {details}")
            msg['Subject'] = subject
            msg['From'] = self.sender_address
            msg['To'] = self.recipient_address

            with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
                server.starttls()
                server.login(self.smtp_username, self.smtp_password)
                server.sendmail(self.sender_address, self.recipient_address, msg.as_string())
            logger.info(f"Email sent successfully to {self.recipient_address}")
        except Exception as e:
            logger.error(f"Failed to send email: {e}")

if __name__ == '__main__':
    # Example usage (replace with actual configuration)
    logging.basicConfig(level=logging.INFO)
    config = {
        'enabled': False, # Set to True to enable email
        'sender_address': 'aiva@example.com',
        'recipient_address': 'admin@example.com',
        'smtp_server': 'smtp.example.com',
        'smtp_port': 587,
        'smtp_username': 'aiva',
        'smtp_password': 'password'
    }

    email_sender = EmailSender(config)
    email_sender.send_email(
        subject='Test Email from AIVA',
        body='This is a test email to verify the email sending functionality.',
        details={'test_key': 'test_value'}
    )
