from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/observability-contract-matrix")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "observability_contract_matrix_v1"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["program_status"]["current_task_id"] == "P1-003"
    assert data["program_status"]["next_task_id"] == "P1-004"
    assert data["program_status"]["read_only_mode"] is True
    assert data["rollups"]["total_surfaces"] == len(data["observability_surfaces"]) == 6
    assert data["rollups"]["noise_reduction_opportunities_total"] == 5
    assert data["rollups"]["surface_ids_requiring_action"] == [
        "grafana_dashboard_inventory",
        "prometheus_alert_rule_catalog",
    ]
    assert data["rollups"]["approval_required_opportunity_ids"] == [
        "alertmanager_grouping_inhibit_tuning",
        "prometheus_noise_rule_tuning",
    ]
    assert data["operation_boundaries"]["read_only_api_allowed"] is True
    assert data["operation_boundaries"]["prometheus_rule_write_allowed"] is False
    assert data["operation_boundaries"]["alertmanager_route_write_allowed"] is False
    assert data["operation_boundaries"]["alertmanager_to_openclaw_allowed"] is False
    assert data["operation_boundaries"]["silence_create_allowed"] is False
    assert data["operation_boundaries"]["grafana_dashboard_write_allowed"] is False
    assert data["operation_boundaries"]["notification_send_allowed"] is False
    assert data["operation_boundaries"]["deploy_trigger_allowed"] is False
    assert data["approval_boundaries"]["prometheus_rule_change_authorized"] is False
    assert data["approval_boundaries"]["alertmanager_to_openclaw_authorized"] is False
    assert data["approval_boundaries"]["deploy_reload_authorized"] is False
    alertmanager = next(
        row for row in data["observability_surfaces"] if row["surface_id"] == "alertmanager_awoooi_route"
    )
    assert alertmanager["status"] == "verified"
    assert alertmanager["noise_policy_status"] == "proposal_only"
    assert "OpenClaw 只做 AI 分析" in alertmanager["coverage_contract"]
    assert "Alertmanager 指向 OpenClaw receiver 批准" in data["operator_contract"]["must_not_interpret_as"]
    assert "不接收 Alertmanager webhook" in data["operator_contract"]["alertmanager_route_policy"]
    for opportunity in data["noise_reduction_opportunities"]:
        assert opportunity["proposal_only"] is True
