"""
FastAPI Dependencies for Procurement Service
"""

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

from .database import get_db


async def get_current_tenant_id(
    x_tenant_id: Optional[str] = Header(None, alias="X-Tenant-ID")
) -> UUID:
    """
    Extract and validate tenant ID from request header.

    Args:
        x_tenant_id: Tenant ID from X-Tenant-ID header

    Returns:
        UUID: Validated tenant ID

    Raises:
        HTTPException: If tenant ID is missing or invalid
    """
    if not x_tenant_id:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="X-Tenant-ID header is required"
        )

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


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