"""Common API dependencies."""
from __future__ import annotations

from uuid import UUID

from fastapi import Depends, Query, Request
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.database import get_db
from app.core.security import get_current_user
from shared.exceptions import TenantNotResolvedError
from shared.pagination import PaginationParams
from shared.security import CurrentUser
from shared.tenant import STATE_TENANT_ID

__all__ = [
    "get_db",
    "get_pagination",
    "require_tenant",
    "get_current_user",
    "AsyncSession",
    "CurrentUser",
]


def get_pagination(
    page: int = Query(default=1, ge=1),
    page_size: int = Query(default=20, ge=1, le=500),
) -> PaginationParams:
    return PaginationParams(page=page, page_size=page_size)


def require_tenant(request: Request) -> UUID:
    tenant_id = getattr(request.state, STATE_TENANT_ID, None)
    if tenant_id is None:
        raise TenantNotResolvedError(
            "tenant قابل تشخیص نبود. هدر X-Tenant-ID را ارسال کنید."
        )
    return tenant_id
