import logging
import httpx
from app.configs import settings

log = logging.getLogger(__name__)

message = """
A guest requested to cancel their reservation through the chatbot.

Confirmation Number: {confirmation_number}

Please review this reservation and contact the guest if necessary.

This request was forwarded automatically by the chatbot.
"""

async def send_email(confirmation_number: str):

    try: 
        domain = settings.MAILGUN_SANDBOX_DOMAIN
        api_key = settings.MAILGUN_SANDBOX_SENDING_API_KEY

        url = f"https://api.mailgun.net/v3/{domain}/messages"

        subject = f"Reservation Cancellation Request - {confirmation_number}"

        data = {
            "from": settings.FROM_EMAIL,
            "to": [settings.FRONTDESK_EMAIL],
            "subject": subject,
            "text": message.format(confirmation_number=confirmation_number)
        }

        async with httpx.AsyncClient() as client:
            response = await client.post(
                url,
                auth=("api", api_key),
                data=data
            )

        if response.status_code in [200, 202]:
            return True

        return False
    
    except Exception as e:
        log.error(f"Error sending email: {e}")
        return False