import logging
import httpx
from app.configs import settings

log = logging.getLogger(__name__)

message = """
<table cellpadding="0" cellspacing="0"
style="border:1px solid #e0e0e0;max-width:600px;margin:auto;font-family:Arial,Helvetica,sans-serif;color:#333333;font-size:13px"
width="100%">
<tbody>
<tr style="width:100%">
<td style="margin:0px;background:#20638f;padding:0px 20px 0px">
<p style="width:46%;float:left;padding:0px;margin:10px 0px 8px;text-align:left">
<img src="https://www.lodgingsystem.com/ls-api/assets/uploads/email/minerva3_logo.png"
alt="Lodging system" title="Lodging system" style="width:66%;min-width:180px">
</p>
<div style="width:auto;float:right;color:#fff;padding:0px 0px">
<p style="width:100%;float:left;padding:6px 0px;margin:0;text-align:right">
<img src="https://www.lodgingsystem.com/ls-api/assets/img/email-templates/res_can.png"
alt="Reservation created" title="Reservation created" style="width:30px">
</p>
</div>
<span style="padding:0px;margin:0;margin-bottom:10px;text-align:left;font-size:14px;float:left;width:100%">
<span style="width:auto;color:#fff">A new cancellation request has been received
</span>
</span>
</td>
</tr>
<tr>
<td align="center" style="padding:30px 20px 20px;">
 
<!-- Cancel Image -->
<img src="https://www.lodgingsystem.com/ls-api/assets/img/email-templates/cancel-img.png" alt="Reservation Cancel Request" width="200"
style="display:block;max-width:200px;width:100%;margin:0 auto;">
 
<!-- Heading -->
<div
style="font-family:Arial, Helvetica, sans-serif;font-size:20px;line-height:24px;font-weight:700;color:#20638f;text-align:center;margin-top:16px;">
A guest requested to cancel<br>
their reservation through the chatbot.
</div>
 
<!-- Divider -->
<table width="100%" cellpadding="0" cellspacing="0" border="0"
style="max-width:400px;margin-top:20px;margin-bottom: 20px;">
<tr>
<td height="1" style="background:#DCE5EF; line-height: 0px;"></td>
</tr>
</table>
 
<!-- Confirmation Number Box -->
<table width="100%" cellpadding="0" cellspacing="0" border="0"
style="max-width:500px;border:1px solid #DCE5EF;border-radius:8px;background:#FFFFFF;">
<tr>
<td style="padding:18px;">
 
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
 
<!-- Icon -->
<td width="70" valign="middle">
<div style="
text-align:center;
line-height:60px;
">
<img src="https://www.lodgingsystem.com/ls-api/assets/img/email-templates/confirmation-icon.png" width="60"
style="vertical-align:middle;">
</div>
</td>
 
<!-- Text -->
<td valign="middle" style="
font-family:Arial, Helvetica, sans-serif;
">
<div style="
font-size:16px;
color:#3D434C;
font-weight:700;
margin-bottom:8px;
">
Confirmation Number:
</div>
 
<div style="
font-size:18px;
color:#20638f;
font-weight:700;
">
{confirmation_number}
</div>
</td>
 
</tr>
</table>
 
</td>
</tr>
</table>
 
<!-- Bottom Divider -->
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:400px;margin-top:20px;">
<tr>
<td height="1" style="background:#DCE5EF; line-height: 0px;"></td>
</tr>
</table>
 
<!-- Footer Text -->
<div style="font-family:Arial, Helvetica, sans-serif;font-size:16px;line-height:24px;color:#5F6773;text-align:center;margin-top:20px;">
Please review this reservation and contact the guest if necessary.<br>
This request was forwarded automatically by the chatbot.
</div>
 
</td>
</tr>


<tr style="width:100%">
<td style="margin:0px;background:#20638f;padding:0px 20px 0px">
<p style="width:100%;padding:0px;margin:10px 0 5px;text-align:center;font-size:16px;color:#fff">
<span>Thanks For Choosing</span>
</p>
<p style="width:100%;float:left;padding:0px;margin:0px 0px 8px;text-align:center">
<img src="https://www.lodgingsystem.com/ls-api/assets/uploads/email/minerva3_logo.png" width="150px"
alt="Lodging system" title="Lodging system" style="width:150px" class="CToWUd">
</p>
</td>
</tr>
 
</tbody>
</table> 
"""

async def send_email(confirmation_number: str):

    try: 
        domain = settings.MAILGUN_DOMAIN
        api_key = settings.MAILGUN_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,
            "html": message.format(confirmation_number=confirmation_number)
        }

        async with httpx.AsyncClient(timeout=60.0) 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