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.full_chain

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


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

    try:
        result = _RUNTIME.run_full_chain(case)
    except ValueError as exc:
        pytest.skip(str(exc))

    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}"
    assert (result.llm_answer or "").strip(), "LLM answer must not be empty"
