from __future__ import annotations

from dataclasses import dataclass
from typing import Protocol


class ModuleTraceLogger(Protocol):
    def log_module(self, request_id: str, module: str, title: str, payload: dict | None = None) -> None: ...


@dataclass(frozen=True, slots=True)
class ModuleTrace:
    request_id: str
    module: str
    logger: ModuleTraceLogger

    def log(self, title: str, payload: dict | None = None) -> None:
        self.logger.log_module(self.request_id, self.module, title, payload or {})


@dataclass(frozen=True, slots=True)
class RequestTraceContext:
    request_id: str
    logger: ModuleTraceLogger

    def module(self, name: str) -> ModuleTrace:
        return ModuleTrace(request_id=self.request_id, module=name, logger=self.logger)
