"""
FastAPI Dependencies for Procurement Service
Uses shared authentication infrastructure with UUID validation
"""

from fastapi import Depends, HTTPException, status
from uuid import UUID
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncSession

from .database import get_db
from shared.auth.decorators import get_current_tenant_id_dep


async def get_current_tenant_id(
    tenant_id: Optional[str] = Depends(get_current_tenant_id_dep)
) -> UUID:
    """
    Extract and validate tenant ID from request using shared infrastructure.
    Adds UUID validation to ensure tenant ID format is correct.

    Args:
        tenant_id: Tenant ID from shared dependency

    Returns:
        UUID: Validated tenant ID

    Raises:
        HTTPException: If tenant ID is missing or invalid UUID format
    """
    if not tenant_id:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="x-tenant-id header is required"
        )

    try:
        return UUID(tenant_id)
    except (ValueError, AttributeError):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"Invalid tenant ID format: {tenant_id}"
        )


# Re-export get_db for convenience
__all__ = ["get_db", "get_current_tenant_id"]
