"""Contract tests: ARIAAuditBackend satisfies AuditBackend Protocol.

Imports real AGT types to verify structural compatibility with
the agent_os.audit_logger.AuditBackend protocol.
"""

from __future__ import annotations

import httpx
import pytest
from agent_os.audit_logger import AuditEntry, GovernanceAuditLogger

from aria_agentkit.audit.backend import ARIAAuditBackend


@pytest.fixture
def audit_backend() -> ARIAAuditBackend:
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(status_code=201, json={"status": "ok"})

    return ARIAAuditBackend(
        receipt_vault_url="http://localhost:9000",
        http_client=httpx.Client(transport=httpx.MockTransport(handler)),
    )


@pytest.mark.contract
class TestAuditBackendProtocol:
    def test_has_write_and_flush_methods(self, audit_backend: ARIAAuditBackend) -> None:
        """AuditBackend Protocol requires write() and flush() methods."""
        assert hasattr(audit_backend, "write")
        assert callable(audit_backend.write)
        assert hasattr(audit_backend, "flush")
        assert callable(audit_backend.flush)

    def test_write_accepts_audit_entry(self, audit_backend: ARIAAuditBackend) -> None:
        entry = AuditEntry(
            event_type="test",
            agent_id="a1",
            action="tool.invoke",
            decision="allow",
        )
        audit_backend.write(entry)
        assert audit_backend.pending_count() == 1

    def test_flush_is_callable(self, audit_backend: ARIAAuditBackend) -> None:
        audit_backend.flush()

    def test_integrates_with_governance_audit_logger(
        self, audit_backend: ARIAAuditBackend
    ) -> None:
        """Prove the backend works when plugged into AGT's GovernanceAuditLogger."""
        logger = GovernanceAuditLogger()
        logger.add_backend(audit_backend)
        logger.log_decision(
            agent_id="a1",
            action="file_read",
            decision="allow",
            reason="policy matched",
        )
        assert audit_backend.pending_count() == 1
        audit_backend.flush()
        assert audit_backend.pending_count() == 0
