"""
Webhook routes for API Gateway - Handles webhook endpoints

Route Configuration Notes:
- Stripe configures webhook URL as: https://domain.com/api/v1/webhooks/stripe
- Gateway receives /api/v1/webhooks/* routes and proxies to tenant service at /webhooks/*
- Gateway routes use /api/v1 prefix, but tenant service routes use /webhooks/* prefix
"""

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

from app.core.config import settings
from app.core.header_manager import header_manager

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

# ================================================================
# WEBHOOK ENDPOINTS - Direct routing to tenant service
# All routes use /api/v1 prefix for consistency
# ================================================================

# Stripe webhook endpoint
@router.post("/api/v1/webhooks/stripe")
async def proxy_stripe_webhook(request: Request):
    """Proxy Stripe webhook requests to tenant service (path: /webhooks/stripe)"""
    logger.info("Received Stripe webhook at /api/v1/webhooks/stripe")
    return await _proxy_to_tenant_service(request, "/webhooks/stripe")

# Generic webhook endpoint
@router.post("/api/v1/webhooks/generic")
async def proxy_generic_webhook(request: Request):
    """Proxy generic webhook requests to tenant service (path: /webhooks/generic)"""
    return await _proxy_to_tenant_service(request, "/webhooks/generic")

# ================================================================
# PROXY HELPER FUNCTIONS
# ================================================================

async def _proxy_to_tenant_service(request: Request, target_path: str):
    """Proxy request to tenant service"""
    return await _proxy_request(request, target_path, settings.TENANT_SERVICE_URL)

async def _proxy_request(request: Request, target_path: str, service_url: str):
    """Generic proxy function with enhanced error handling"""

    # Handle OPTIONS requests directly for CORS
    if request.method == "OPTIONS":
        return Response(
            status_code=200,
            headers={
                "Access-Control-Allow-Origin": settings.CORS_ORIGINS_LIST,
                "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
                "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Tenant-ID, Stripe-Signature",
                "Access-Control-Allow-Credentials": "true",
                "Access-Control-Max-Age": "86400"
            }
        )

    try:
        url = f"{service_url}{target_path}"

        # Use unified HeaderManager for consistent header forwarding
        headers = header_manager.get_all_headers_for_proxy(request)

        # Debug logging
        logger.info(f"Forwarding webhook request to {url}")

        # Get request body if present
        body = None
        if request.method in ["POST", "PUT", "PATCH"]:
            body = await request.body()

        # Add query parameters
        params = dict(request.query_params)

        timeout_config = httpx.Timeout(
            connect=30.0,
            read=60.0,
            write=30.0,
            pool=30.0
        )

        async with httpx.AsyncClient(timeout=timeout_config) as client:
            response = await client.request(
                method=request.method,
                url=url,
                headers=headers,
                content=body,
                params=params
            )

            # Handle different response types
            if response.headers.get("content-type", "").startswith("application/json"):
                try:
                    content = response.json()
                except:
                    content = {"message": "Invalid JSON response from service"}
            else:
                content = response.text

            return JSONResponse(
                status_code=response.status_code,
                content=content
            )

    except Exception as e:
        logger.error(f"Unexpected error proxying webhook request to {service_url}{target_path}: {e}")
        raise HTTPException(
            status_code=500,
            detail="Internal gateway error"
        )