import os

import pytest
from httpx import ASGITransport, AsyncClient

os.environ.setdefault("DATABASE_URL", "postgresql+asyncpg://test:test@localhost/test")

from src.main import app


@pytest.mark.asyncio
async def test_get_agent_result_capture_writer_dry_run_readback_api() -> None:
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.get("/api/v1/agents/agent-result-capture-writer-dry-run-readback")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_result_capture_writer_dry_run_readback_v1"
    assert data["program_status"]["current_task_id"] == "P2-124"
    assert data["program_status"]["next_task_id"] == "P2-125"
    assert data["program_status"]["overall_completion_percent"] == 100

    rollups = data["rollups"]
    assert rollups["dry_run_readback_card_count"] == 5
    assert rollups["receipt_verifier_check_count"] == 5
    assert rollups["promotion_readiness_gate_count"] == 5
    assert rollups["blocked_promotion_write_count"] == 6
    assert rollups["operator_action_count"] == 5
    assert rollups["critical_blocker_count"] == 4
    assert rollups["owner_approval_received_count"] == 0
    assert rollups["dual_approval_received_count"] == 0
    assert rollups["dry_run_hash_verified_count"] == 0
    assert rollups["receipt_verifier_pass_count"] == 0
    assert rollups["writer_apply_count"] == 0
    assert rollups["dry_run_execution_count"] == 0
    assert rollups["receipt_write_count"] == 0
    assert rollups["promotion_apply_count"] == 0
    assert rollups["result_capture_write_count"] == 0
    assert rollups["learning_write_count"] == 0
    assert rollups["playbook_trust_write_count"] == 0
    assert rollups["gateway_queue_write_count"] == 0
    assert rollups["telegram_send_count"] == 0
    assert rollups["production_write_count"] == 0

    assert {card["readback_only"] for card in data["dry_run_readback_cards"]} == {True}
    assert {card["runtime_write_enabled"] for card in data["dry_run_readback_cards"]} == {False}
    assert {card["dry_run_execution_enabled"] for card in data["dry_run_readback_cards"]} == {False}
    assert {gate["promotion_allowed"] for gate in data["promotion_readiness_gates"]} == {False}
