"""
api.deps — Dependency injection for FastAPI routes.
"""
from __future__ import annotations
from fastapi import Header, HTTPException, Request

from agentpaas.api.middleware.auth import authenticate, AuthError, TenantContext
from agentpaas.db.session import get_db


async def get_tenant(authorization: str = Header(default="")) -> TenantContext:
    """FastAPI dependency: extract and verify API key, return tenant context."""
    try:
        return await authenticate(authorization)
    except AuthError as e:
        raise HTTPException(status_code=e.code, detail={"error": {"code": "UNAUTHORIZED", "message": e.message}})


async def get_database():
    """FastAPI dependency: get database connection."""
    return get_db()
