from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/backup-notification-policy")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "backup_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-103"
    assert data["program_status"]["next_task_id"] == "P1-104"
    assert data["rollups"]["total_rules"] == len(data["policy_rules"]) == 8
    assert data["rollups"]["by_decision"]["suppress_immediate_success"] == 2
    assert len(data["rollups"]["immediate_escalation_rule_ids"]) == 4
    assert len(data["rollups"]["suppressed_success_rule_ids"]) == 2
    assert data["daily_summary_contract"]["summary_time_taipei"] == "06:05"
    assert data["daily_summary_contract"]["success_immediate_notifications_allowed"] is False
    assert data["operation_boundaries"]["read_only_policy_allowed"] is True
    assert data["operation_boundaries"]["notification_send_allowed"] is False
    assert data["operation_boundaries"]["backup_execution_allowed"] is False
    assert data["operation_boundaries"]["restore_execution_allowed"] is False
    assert data["operation_boundaries"]["offsite_sync_execution_allowed"] is False
    assert data["operation_boundaries"]["credential_marker_write_allowed"] is False
    assert data["operation_boundaries"]["schedule_change_allowed"] is False
    assert data["operation_boundaries"]["workflow_write_allowed"] is False
    assert data["operation_boundaries"]["telegram_test_message_allowed"] is False
    assert any(rule["rule_id"] == "backup_failed" for rule in data["policy_rules"])
    assert all(
        rule["decision"] == "suppress_immediate_success"
        for rule in data["policy_rules"]
        if rule["backup_state"] == "success"
    )
