from __future__ import annotations

from pathlib import Path

import pytest

from tests.pipeline_setup.suite_02_pipeline.pipeline_intent_rag.helpers.pipeline_config import mode_enabled
from tests.pipeline_setup.suite_02_pipeline.pipeline_intent_rag.helpers.runtime import PipelineRuntime

pytestmark = pytest.mark.router_rag

_TEST_ROOT = Path(__file__).resolve().parent
_RUNTIME = PipelineRuntime(mode="router_rag", test_name="test_intent_router_rag_pipeline", test_root=_TEST_ROOT)
_CASES = _RUNTIME.load_cases()


@pytest.mark.skipif(not mode_enabled("router_rag", _TEST_ROOT), reason="set RUN_INTENT_PIPELINE_ROUTER_RAG=1 to run")
@pytest.mark.parametrize("case", _CASES, ids=lambda item: item.case_id)
def test_intent_router_rag_pipeline(case) -> None:
    pytest.importorskip("sqlalchemy", reason="router_rag integration requires sqlalchemy dependency")

    result = _RUNTIME.run_router_rag(case)

    if case.expected_intent:
        assert result.intent == case.expected_intent
    if case.expect_non_empty_rag:
        assert result.rag_rows, f"RAG returned empty list for case={case.case_id} rag_session_id={result.rag_session_id}"
