from types import SimpleNamespace

from src.models.approval import ApprovalStatus, RiskLevel
from src.services.approval_db import build_approval_created_timeline_event


def test_pending_approval_timeline_event_exposes_operator_gate() -> None:
    record = SimpleNamespace(
        id="approval-pending-1",
        action="kubectl rollout restart deployment/api",
        status=ApprovalStatus.PENDING,
        risk_level=RiskLevel.MEDIUM,
        required_signatures=1,
        current_signatures=0,
        requested_by="openclaw",
        incident_id="INC-20260604-TEST01",
    )

    event = build_approval_created_timeline_event(record)

    assert event.event_type == "human"
    assert event.status == "warning"
    assert event.title == "Approval gate waiting for human decision"
    assert event.actor == "openclaw"
    assert event.actor_role == "approval_gate"
    assert event.risk_level == "medium"
    assert event.approval_id == "approval-pending-1"
    assert event.incident_id == "INC-20260604-TEST01"
    assert "stage=approval_required" in event.description
    assert "next_action=operator_approve_or_reject" in event.description
    assert "blocked_reason=waiting_for_required_signatures" in event.description
    assert "auto_or_manual=manual" in event.description
    assert "needs_human=yes" in event.description
    assert "signatures=0/1" in event.description


def test_low_risk_auto_approved_timeline_event_exposes_auto_gate() -> None:
    record = SimpleNamespace(
        id="approval-auto-1",
        action="collect read-only diagnostics",
        status=ApprovalStatus.APPROVED,
        risk_level=RiskLevel.LOW,
        required_signatures=0,
        current_signatures=0,
        requested_by="openclaw",
        incident_id="INC-20260604-TEST02",
    )

    event = build_approval_created_timeline_event(record)

    assert event.event_type == "human"
    assert event.status == "success"
    assert event.title == "Approval gate passed"
    assert event.risk_level == "low"
    assert "stage=approval_auto_approved" in event.description
    assert "next_action=execute_or_verify" in event.description
    assert "blocked_reason=none" in event.description
    assert "auto_or_manual=auto" in event.description
    assert "needs_human=no" in event.description
    assert "signatures=0/0" in event.description
