from __future__ import annotations

from app.core.config import get_settings
from app.main import create_app


def test_openapi_schema_includes_documented_backend_routes() -> None:
    schema = create_app().openapi()

    assert schema["info"]["title"] == get_settings().app_name
    assert any(tag["name"] == "agent-assets" for tag in schema["tags"])
    assert any(tag["name"] == "knowledge" for tag in schema["tags"])
    assert any(tag["name"] == "ocr" for tag in schema["tags"])
    assert any(tag["name"] == "ontology" for tag in schema["tags"])
    assert any(tag["name"] == "orchestrator" for tag in schema["tags"])
    assert any(tag["name"] == "agent-feedback" for tag in schema["tags"])
    assert any(tag["name"] == "analytics" for tag in schema["tags"])

    agent_assets_post = schema["paths"]["/api/v1/agent-assets"]["post"]
    assert agent_assets_post["summary"] == "创建 Agent 资产"
    assert any(param["name"] == "x-actor" for param in agent_assets_post["parameters"])

    knowledge_upload_post = schema["paths"]["/api/v1/knowledge/documents"]["post"]
    assert knowledge_upload_post["summary"] == "上传知识库文档"
    assert "application/octet-stream" in knowledge_upload_post["requestBody"]["content"]

    knowledge_callback_post = schema["paths"][
        "/api/v1/knowledge/documents/{document_id}/onlyoffice/callback"
    ]["post"]
    assert knowledge_callback_post["summary"] == "接收 ONLYOFFICE 回调"
    assert "application/json" in knowledge_callback_post["requestBody"]["content"]

    ocr_post = schema["paths"]["/api/v1/ocr/recognize"]["post"]
    assert ocr_post["summary"] == "识别票据或图片 OCR"
    assert "multipart/form-data" in ocr_post["requestBody"]["content"]

    ontology_parse_post = schema["paths"]["/api/v1/ontology/parse"]["post"]
    assert ontology_parse_post["summary"] == "解析自然语言为语义本体"
    assert "application/json" in ontology_parse_post["requestBody"]["content"]

    orchestrator_run_post = schema["paths"]["/api/v1/orchestrator/run"]["post"]
    assert orchestrator_run_post["summary"] == "运行 Orchestrator 统一调度"
    assert "application/json" in orchestrator_run_post["requestBody"]["content"]

    feedback_post = schema["paths"]["/api/v1/agent-feedback"]["post"]
    assert feedback_post["summary"] == "记录 Agent 操作评价"
    assert "application/json" in feedback_post["requestBody"]["content"]

    analytics_get = schema["paths"]["/api/v1/analytics/system-dashboard"]["get"]
    assert analytics_get["summary"] == "查询系统看板真实指标"

    settings_cache_clear_post = schema["paths"]["/api/v1/settings/cache/clear"]["post"]
    assert settings_cache_clear_post["summary"] == "清理系统缓存"

    root_get = schema["paths"]["/"]["get"]
    assert root_get["summary"] == "服务根检查"
