from __future__ import annotations

from dataclasses import asdict, dataclass
from typing import Any

from router.query_classifier import RouteDecisionFlags
from router.route_selector import RouteSelectionResult
from router.store_sufficiency import StoreSufficiencyResult


@dataclass
class RouteDecisionLog:
    question_id: str
    question_text: str
    parsed_class: str
    decision_flags: dict[str, Any]
    sufficiency_snapshot: dict[str, Any]
    candidate_routes: list[str]
    rejected_routes: dict[str, str]
    chosen_route: str
    execution_mode: str
    batch_job_id: str | None

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


def build_route_decision_log(
    *,
    question_id: str,
    question_text: str,
    parsed_class: str,
    flags: RouteDecisionFlags,
    suff: StoreSufficiencyResult,
    selection: RouteSelectionResult,
    execution_mode: str,
    batch_job_id: str | None,
) -> RouteDecisionLog:
    return RouteDecisionLog(
        question_id=question_id,
        question_text=question_text,
        parsed_class=parsed_class,
        decision_flags=flags.to_dict(),
        sufficiency_snapshot=suff.to_dict(),
        candidate_routes=selection.candidate_routes,
        rejected_routes=selection.rejected_routes,
        chosen_route=selection.chosen_route,
        execution_mode=execution_mode,
        batch_job_id=batch_job_id,
    )

