import pytest

from apps.scenarios.services import (
    ScenarioNotFound,
    get_scenario,
    list_scenario_issues,
    list_scenarios,
)


def test_list_scenarios_loads_five_configs():
    scenarios = list_scenarios()
    assert [scenario["id"] for scenario in scenarios] == [
        "document_review",
        "knowledge_qa",
        "quality_analysis",
        "risk_audit",
        "ticket_assistant",
    ]


def test_get_scenario_returns_full_agent_config():
    scenario = get_scenario("quality_analysis")
    assert scenario["agent"]["role"] == "质量管理专家"
    assert scenario["rag"]["enabled"] is True
    assert scenario["output"]["type"] == "quality_report"
    assert "质量异常分析" in scenario["applicable_questions"][0]


def test_get_scenario_raises_clear_error_for_missing_id():
    with pytest.raises(ScenarioNotFound, match="场景不存在"):
        get_scenario("missing")


def test_home_page_shows_applicable_questions(client):
    response = client.get("/")

    content = response.content.decode("utf-8")
    assert response.status_code == 200
    assert "适用题型" in content
    assert "SOP 问答" in content


def test_list_scenarios_skips_invalid_config_and_collects_issues(settings, tmp_path):
    valid_file = tmp_path / "valid.yaml"
    invalid_file = tmp_path / "invalid.yaml"
    valid_file.write_text(
        """
id: demo_valid
name: 有效场景
description: 用于测试
agent:
  role: 测试助手
  goal: 正常返回
  instructions:
    - 输出结果
rag:
  enabled: false
tools: []
output:
  type: general_answer
audit:
  enabled: true
""".strip(),
        encoding="utf-8",
    )
    invalid_file.write_text(
        """
id: broken
name: 非法场景
description: 缺少 agent.goal
agent:
  role: 测试助手
  instructions:
    - 输出结果
rag:
  enabled: true
tools: []
output:
  type: general_answer
audit:
  enabled: true
""".strip(),
        encoding="utf-8",
    )
    settings.SCENARIO_CONFIG_DIR = tmp_path

    scenarios = list_scenarios()
    issues = list_scenario_issues()

    assert [scenario["id"] for scenario in scenarios] == ["demo_valid"]
    assert len(issues) == 1
    assert issues[0]["file_name"] == "invalid.yaml"
    assert "agent.goal" in issues[0]["message"]


def test_home_page_shows_invalid_scenario_issues_instead_of_500(client, settings, tmp_path):
    valid_file = tmp_path / "valid.yaml"
    invalid_file = tmp_path / "invalid.yaml"
    valid_file.write_text(
        """
id: demo_valid
name: 有效场景
description: 用于测试
agent:
  role: 测试助手
  goal: 正常返回
  instructions:
    - 输出结果
rag:
  enabled: false
tools: []
output:
  type: general_answer
audit:
  enabled: true
""".strip(),
        encoding="utf-8",
    )
    invalid_file.write_text("id: broken\nname: 缺失结构", encoding="utf-8")
    settings.SCENARIO_CONFIG_DIR = tmp_path

    response = client.get("/")

    content = response.content.decode("utf-8")
    assert response.status_code == 200
    assert "有效场景" in content
    assert "配置异常" in content
    assert "invalid.yaml" in content


def test_home_page_uses_four_top_level_product_entries(client):
    response = client.get("/")

    content = response.content.decode("utf-8")
    assert response.status_code == 200
    assert "审核智能体" in content
    assert "资料包" in content
    assert "知识库" in content
    assert "处理历史" in content
