"""Runtime service wiring for the Takumi gateway."""

from __future__ import annotations

from dataclasses import dataclass

from ..admission import AdmissionController
from ..billing.cost import Pricing, default_pricing
from ..credentials.pool import ProviderPoolV2
from ..credentials.store import CredentialStore, InMemoryCredentialStore
from ..pricing.plan_cache import OrgPlanCache
from ..provider_pool import ProviderPool
from ..providers import ProviderAdapter, default_adapters
from ..routing import Router, RouterLiteAdapter
from ..secrets.store import InMemorySecretStore, SecretStore
from ..stores import (
    BillingStore,
    IdempotencyStore,
    InMemoryBillingStore,
    InMemoryIdempotencyStore,
)


@dataclass
class GatewayServices:
    """Bundle of runtime services used by the gateway request pipeline."""

    adapters: dict[str, ProviderAdapter]
    admission: AdmissionController
    billing_store: BillingStore
    idempotency_store: IdempotencyStore
    pricing: Pricing
    provider_pool: ProviderPool | None
    provider_pool_v2: ProviderPoolV2 | None
    credential_store: CredentialStore | None
    secret_store: SecretStore
    router: Router
    plan_cache: OrgPlanCache


_SERVICES: GatewayServices | None = None
_BILL_STORE: BillingStore = InMemoryBillingStore()
_IDEM_STORE: IdempotencyStore = InMemoryIdempotencyStore()


def build_default_services() -> GatewayServices:
    adapters = default_adapters()
    admission = AdmissionController()
    billing_store = InMemoryBillingStore()
    idempotency_store = InMemoryIdempotencyStore()
    pricing = default_pricing()
    secret_store = InMemorySecretStore()
    credential_store = InMemoryCredentialStore(secret_store)
    provider_pool = ProviderPool()
    provider_pool_v2 = ProviderPoolV2(credential_store)
    router = RouterLiteAdapter()
    plan_cache = OrgPlanCache(None, default_tier="T1")

    return GatewayServices(
        adapters=adapters,
        admission=admission,
        billing_store=billing_store,
        idempotency_store=idempotency_store,
        pricing=pricing,
        provider_pool=provider_pool,
        provider_pool_v2=provider_pool_v2,
        credential_store=credential_store,
        secret_store=secret_store,
        router=router,
        plan_cache=plan_cache,
    )


def configure_gateway_services(services: GatewayServices) -> None:
    """Install the shared services used by gateway entrypoints."""

    global _SERVICES, _BILL_STORE, _IDEM_STORE
    _SERVICES = services
    _BILL_STORE = services.billing_store
    _IDEM_STORE = services.idempotency_store


def get_services() -> GatewayServices:
    """Return the configured services, initialising defaults if necessary."""

    global _SERVICES
    if _SERVICES is None:
        configure_gateway_services(build_default_services())
    assert _SERVICES is not None
    return _SERVICES


def get_billing_store() -> BillingStore:
    return _BILL_STORE


def get_idempotency_store() -> IdempotencyStore:
    return _IDEM_STORE


__all__ = [
    "GatewayServices",
    "build_default_services",
    "configure_gateway_services",
    "get_billing_store",
    "get_idempotency_store",
    "get_services",
]
