from fastapi.testclient import TestClient

from src.main import app


_PUBLIC_FORBIDDEN_TERMS = [
    "工作視窗",
    "對話內容",
    "批准！繼續",
    "In app browser",
    "My request for Codex",
    "browser_context",
    "codex_user_message",
    "prompt_text",
    "raw prompt",
    "raw_prompt",
    "raw payload",
    "raw_payload",
    "private reasoning",
    "chain_of_thought",
    "authorization header",
    "authorization_header",
    "secret value",
    "secret_value",
]


def _collect_strings(value):
    if isinstance(value, str):
        return [value]
    if isinstance(value, list):
        strings = []
        for item in value:
            strings.extend(_collect_strings(item))
        return strings
    if isinstance(value, dict):
        strings = []
        for item in value.values():
            strings.extend(_collect_strings(item))
        return strings
    return []


def test_get_ai_agent_report_status_board_api():
    client = TestClient(app)
    response = client.get("/api/v1/agents/agent-report-status-board")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_report_status_board_v1"
    assert data["program_status"]["current_task_id"] == "P2-108"
    assert data["program_status"]["next_task_id"] == "P2-109"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["report_completion_truth"]["daily_report_visible"] is True
    assert data["report_completion_truth"]["weekly_report_visible"] is True
    assert data["report_completion_truth"]["monthly_report_visible"] is True
    assert data["report_completion_truth"]["per_agent_status_visible"] is True
    assert data["report_completion_truth"]["live_report_delivery_enabled"] is False
    assert data["report_completion_truth"]["live_telegram_send_count_24h"] == 0
    assert data["report_completion_truth"]["live_auto_optimization_count_24h"] == 0
    assert data["rollups"]["report_card_count"] == 3
    assert data["rollups"]["agent_status_count"] == 3
    assert data["rollups"]["visible_chart_count"] == 3
    assert data["rollups"]["operator_answer_count"] == 4
    assert data["rollups"]["workload_unit_total"] == 91
    assert data["rollups"]["live_delivery_count"] == 0
    assert data["rollups"]["live_runtime_work_units"] == 0
    assert data["rollups"]["live_auto_optimization_count"] == 0


def test_get_ai_agent_report_status_board_api_redacts_public_terms():
    client = TestClient(app)
    response = client.get("/api/v1/agents/agent-report-status-board")

    assert response.status_code == 200
    all_text = "\n".join(_collect_strings(response.json()))
    for term in _PUBLIC_FORBIDDEN_TERMS:
        assert term not in all_text
