import logging

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, EmailStr

from app.services.email_service import send_contact_request_email

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/contact", tags=["Contact"])


class ContactRequest(BaseModel):
    name: str
    email: EmailStr
    phone: str | None = None
    company: str | None = None
    message: str | None = None


class ContactResponse(BaseModel):
    detail: str


@router.post("/", response_model=ContactResponse, status_code=200)
async def create_contact_request(contact_data: ContactRequest):
    """
    Handle contact request from landing page form.
    Sends an email notification instead of saving to database.
    """
    try:
        await send_contact_request_email(
            name=contact_data.name,
            email=contact_data.email,
            phone=contact_data.phone,
            company=contact_data.company,
            message=contact_data.message,
        )
        logger.info(f"Contact request email sent for {contact_data.email}")
        return ContactResponse(detail="Contact request submitted successfully")
    except Exception as e:
        logger.error(f"Failed to send contact request email: {e}")
        raise HTTPException(
            status_code=500, detail="Failed to submit contact request. Please try again."
        )
