import os

import boto3
from botocore.exceptions import ClientError

AWS_REGION = os.getenv("COGNITO_REGION", "us-west-1")


def send_email(
    sender: str,
    recipients: list[str],
    subject: str,
    body_text: str = None,
    body_html: str = None,
    cc_recipients: list[str] = None,
):
    # Create a new SES resource
    client = boto3.client(
        "ses", region_name=AWS_REGION
    )  # Make sure to use your SES region

    # Try sending the email
    message_body = {"Subject": {"Data": subject}, "Body": {}}
    if body_text:
        message_body["Body"]["Text"] = {"Data": body_text}
    if body_html:
        message_body["Body"]["Html"] = {"Data": body_html}
    try:
        response = client.send_email(
            Source=sender,
            Destination={
                "ToAddresses": recipients,
                "CcAddresses": [] if cc_recipients is None else cc_recipients,
            },
            Message=message_body,
        )
    except ClientError as e:
        print(f"Error sending email: {e.response['Error']['Message']}")
    else:
        print(f"Email sent! Message ID: {response['MessageId']}")


if __name__ == "__main__":
    # Example usage
    send_email(
        sender="sagar.soni@xcapmarket.com",
        recipients=["sagar.soni@xcapmarket.com"],
        subject="Test Email",
        # body_text="This is a test email sent from AWS SES using boto3.",
        body_html="<html><body><h1>This is a test email sent from AWS SES using boto3.</h1></body></html>",
    )
