from __future__ import annotations

import logging

from app.modules.rag.intent_router_v2.models import ConversationState, IntentRouterResult, RepoContext
from app.modules.rag.intent_router_v2.router import IntentRouterV2

LOGGER = logging.getLogger(__name__)


class IntentRouterScenarioRunner:
    def __init__(self, router: IntentRouterV2) -> None:
        self._router = router

    def run(self, queries: list[str], repo_context: RepoContext | None = None) -> list[IntentRouterResult]:
        state = ConversationState()
        context = repo_context or RepoContext()
        results: list[IntentRouterResult] = []
        for index, user_query in enumerate(queries, start=1):
            LOGGER.warning("intent router local input: turn=%s user_query=%s", index, user_query)
            result = self._router.route(user_query, state, context)
            LOGGER.warning("intent router local output: turn=%s result=%s", index, result.model_dump_json(ensure_ascii=False))
            results.append(result)
            state = state.advance(result)
        return results
