"""
Notification routes for gateway
"""

from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import JSONResponse
import httpx
import logging

from app.core.config import settings

logger = logging.getLogger(__name__)
router = APIRouter()

@router.post("/send")
async def send_notification(request: Request):
    """Proxy notification request to notification service"""
    try:
        body = await request.body()
        auth_header = request.headers.get("Authorization")
        
        async with httpx.AsyncClient(timeout=10.0) as client:
            response = await client.post(
                f"{settings.NOTIFICATION_SERVICE_URL}/send",
                content=body,
                headers={
                    "Content-Type": "application/json",
                    "Authorization": auth_header
                }
            )
            
            return JSONResponse(
                status_code=response.status_code,
                content=response.json()
            )
            
    except httpx.RequestError as e:
        logger.error(f"Notification service unavailable: {e}")
        raise HTTPException(
            status_code=503,
            detail="Notification service unavailable"
        )

@router.get("/history")
async def get_notification_history(request: Request):
    """Get notification history"""
    try:
        auth_header = request.headers.get("Authorization")
        
        async with httpx.AsyncClient(timeout=10.0) as client:
            response = await client.get(
                f"{settings.NOTIFICATION_SERVICE_URL}/history",
                headers={"Authorization": auth_header}
            )
            
            return JSONResponse(
                status_code=response.status_code,
                content=response.json()
            )
            
    except httpx.RequestError as e:
        logger.error(f"Notification service unavailable: {e}")
        raise HTTPException(
            status_code=503,
            detail="Notification service unavailable"
        )