import httpx
import logging
from app.configs.settings import settings
from app.utils.constants import get_hotel_details

logger = logging.getLogger(__name__)

async def special_request(email: str, confirmation_number: str, special_request: str, hotel_id: str, sub_hotel_id: str):
    try:
        async with httpx.AsyncClient(timeout=60.0) as client:
            response = await client.post(
                settings.LS_API_URL + "frontend/Reservations/specialRequest",
                json={
                    "email": email,
                    "confirmation_number": confirmation_number,
                    "special_request": special_request,
                },
                headers={
                    "hotel_id": hotel_id,
                    "sub_hotel_id": sub_hotel_id,
                })
            data = response.json()
            
            if data.get("success"):
                return (
                    "\n\nYour special request has been successfully added to your reservation. "
                    "While we'll do our best to accommodate it, fulfillment is subject to availability. "
                    "Is there anything else I can assist you with today?"
                )
            else:
                return (
                    "\n\nI couldn't locate a reservation with the email address and confirmation number you provided. "
                    "Please double-check both details and try again. "
                    "Is there anything else I can assist you with?"
                )
    except Exception as e:
        logger.error(f"Error adding special request: {e}")
        HOTEL_INFO = await get_hotel_details()
        hotel_data = HOTEL_INFO[hotel_id]
        return (
            "\n\nI'm sorry, looks like system is not able to add your special request at the moment. Please try again after some time, or contact the front desk directly for assistance. "
            f"Name: {hotel_data['hotel_name']}\nAddress: {hotel_data['hotel_address']}\nPhone: {hotel_data['hotel_phone']}\nEmail: {hotel_data['hotel_email']}\n"
            "\n\nIs there anything else I can assist you with?"
        )
