from __future__ import annotations

import json

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


def test_service_health_failure_notification_policy_endpoint_returns_committed_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

    response = client.get("/api/v1/agents/service-health-failure-notification-policy")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "service_health_failure_notification_policy_v1"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["program_status"]["read_only_mode"] is True
    assert data["program_status"]["current_task_id"] == "P1-007"
    assert data["program_status"]["next_task_id"] == "P2-004"
    assert data["rollups"]["total_rules"] == len(data["policy_rules"]) == 7
    assert data["rollups"]["by_decision"]["suppress_immediate_success"] == 2
    assert data["rollups"]["by_decision"]["escalate_immediate"] == 3
    assert len(data["rollups"]["immediate_escalation_rule_ids"]) == 3
    assert len(data["rollups"]["suppressed_success_rule_ids"]) == 2
    assert len(data["rollups"]["action_required_rule_ids"]) == 2
    assert data["rollups"]["notification_send_allowed_count"] == 0
    assert data["operation_boundaries"]["read_only_policy_allowed"] is True
    assert data["operation_boundaries"]["notification_send_allowed"] is False
    assert data["operation_boundaries"]["telegram_test_message_allowed"] is False
    assert data["operation_boundaries"]["awooop_event_write_allowed"] is False
    assert data["operation_boundaries"]["live_probe_allowed"] is False
    assert data["operation_boundaries"]["service_restart_allowed"] is False
    assert data["operation_boundaries"]["endpoint_change_allowed"] is False
    assert data["operation_boundaries"]["runtime_execution_allowed"] is False
    assert "blocked_reason" in data["message_template_contract"]["required_fields"]
    assert "secret_value" in data["message_template_contract"]["forbidden_fields"]
    assert "work_window_transcript" in data["message_template_contract"]["forbidden_fields"]
    assert data["display_redaction_contract"]["conversation_transcript_display_allowed"] is False
    assert data["display_redaction_contract"]["redaction_required"] is True
    assert "未核准內部內容" in data["display_redaction_contract"]["forbidden_frontend_content"]
    assert all(
        rule["decision"] == "suppress_immediate_success"
        for rule in data["policy_rules"]
        if rule["service_state"] == "verified"
    )

    serialized = json.dumps(data, ensure_ascii=False)
    for marker in [
        "批准！繼續",
        "My request for Codex",
        "Current URL:",
        "# In app browser",
        "AGENTS.md instructions",
    ]:
        assert marker not in serialized
