"""Transient-failure wrapper for AuditService used in BDD tests.

Wraps a real ``AuditService`` instance and forces the **first**
``record()`` call to raise, simulating a transient database error.
Subsequent calls delegate to the real service.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from cleveragents.application.services.audit_service import AuditService


class TransientFailAuditService:
    """Wraps AuditService to fail on the first record() call, then succeed."""

    def __init__(self, real_service: AuditService) -> None:
        self._real = real_service
        self._call_count = 0

    def record(self, **kwargs: Any) -> Any:
        self._call_count += 1
        if self._call_count == 1:
            raise RuntimeError("Transient DB error")
        return self._real.record(**kwargs)

    def __getattr__(self, name: str) -> Any:
        return getattr(self._real, name)
