# SPDX-FileCopyrightText: Magenta ApS <https://magenta.dk>
# SPDX-License-Identifier: MPL-2.0
"""FastRAMQPI Context."""

from typing import Any
from typing import AsyncContextManager
from typing import Awaitable
from typing import Callable
from typing import TypedDict

from authlib.integrations.httpx_client import AsyncOAuth2Client
from fastapi import FastAPI
from gql.client import AsyncClientSession
from prometheus_fastapi_instrumentator import Instrumentator
from pydantic import BaseSettings
from sqlalchemy.ext.asyncio import AsyncEngine
from sqlalchemy.ext.asyncio import async_sessionmaker

from .raclients.graph.client import GraphQLClient
from .raclients.modelclient.mo import ModelClient
from .ramqp.mo import MOAMQPSystem

HealthcheckFunction = Callable[["Context"], Awaitable[bool]]


class Context(TypedDict, total=False):
    """Execution context."""

    name: str
    settings: BaseSettings
    lifespan_managers: dict[int, set[AsyncContextManager]]
    app: FastAPI
    instrumentator: Instrumentator
    amqpsystem: MOAMQPSystem
    engine: AsyncEngine
    sessionmaker: async_sessionmaker

    # Legacy clients
    legacy_graphql_client: GraphQLClient
    legacy_graphql_session: AsyncClientSession
    legacy_model_client: ModelClient

    # Authenticated HTTPX Client
    mo_client: AsyncOAuth2Client

    # Autogenerated GraphQL Client
    # We cannot type the autogenerated GraphQL client since its signature depends on
    # the specific queries required by the integration.
    graphql_client: Any

    user_context: dict[str, Any]
