from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/delivery-closure-workbench")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "delivery_closure_workbench_v1"
    assert data["summary"]["source_count"] == 5
    assert data["summary"]["loaded_source_count"] == 5
    assert data["summary"]["runtime_execution_authorized"] is False
    assert data["summary"]["remote_write_authorized"] is False
    assert data["summary"]["repo_creation_authorized"] is False
    assert data["summary"]["refs_sync_authorized"] is False
    assert data["summary"]["workflow_trigger_authorized"] is False
    assert data["summary"]["secret_values_collected"] is False
    assert data["summary"]["average_completion_percent"] >= 0
    assert data["summary"]["high_risk_blocker_count"] > 0

    lanes = {lane["id"]: lane for lane in data["lanes"]}
    sources = {source["id"]: source for source in data["source_statuses"]}
    assert sorted(lanes) == ["backup", "gitea", "github", "release", "runtime"]
    assert lanes["release"]["metric"]["kind"] == "blocked_gate"
    assert lanes["github"]["metric"]["kind"] == "private_backup_verified"
    assert lanes["gitea"]["metric"]["kind"] == "workflow_count"
    assert lanes["runtime"]["metric"]["kind"] == "surface_count"
    assert lanes["backup"]["metric"]["kind"] == "readiness_row_count"
    assert sources["github_private_backup"]["loaded"] is True
    assert sources["github_private_backup"]["schema_version"] == "github_target_private_backup_evidence_gate_v1"
    assert sources["github_private_backup"]["missing_reason"] == ""
    assert lanes["github"]["blocker_count"] == 9
    assert lanes["github"]["status"] == "blocked_public_visibility_and_safe_credential_evidence_required"
    assert lanes["github"]["metric"]["verified"] == 0
    assert lanes["github"]["metric"]["total"] == 9
    assert all(0 <= lane["completion_percent"] <= 100 for lane in lanes.values())
    assert all(lane["tone"] in {"ok", "warn", "danger"} for lane in lanes.values())

    boundaries = data["operation_boundaries"]
    assert boundaries["read_only_api_allowed"] is True
    assert boundaries["runtime_write_allowed"] is False
    assert boundaries["remote_write_allowed"] is False
    assert boundaries["repo_creation_allowed"] is False
    assert boundaries["visibility_change_allowed"] is False
    assert boundaries["refs_sync_allowed"] is False
    assert boundaries["workflow_trigger_allowed"] is False
    assert boundaries["secret_value_collection_allowed"] is False
    assert boundaries["backup_restore_execution_allowed"] is False
    assert boundaries["active_scan_allowed"] is False

    assert "192.168.0." not in response.text
