"""Tenant-aware request context primitives.

The gateway creates one :class:`RequestContext` per incoming request and makes
it available to downstream platform components. Existing apps can remain
unaware of the platform layer while integration points consume this context.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass(slots=True)
class RequestContext:
    """Normalized metadata attached to a request before app dispatch."""

    request_id: str
    tenant: dict[str, Any] | None
    app: str | None
    db: dict[str, Any] | None
    storage: dict[str, Any] | None
    limits: dict[str, Any] = field(default_factory=dict)
    auth: dict[str, Any] = field(default_factory=dict)
    extras: dict[str, Any] = field(default_factory=dict)

    def as_dict(self) -> dict[str, Any]:
        """Return a serializable representation for logs and debugging."""
        return {
            "request_id": self.request_id,
            "tenant": self.tenant,
            "app": self.app,
            "db": self.db,
            "storage": self.storage,
            "limits": self.limits,
            "auth": self.auth,
            "extras": self.extras,
        }

