"""A mock AnchorBackend that simulates A0 → A2 without a live substrate — for proving the cotrugli-side
batching + verification + negative control. `wrong_root` makes it anchor a DIFFERENT root than submitted, so
the independent verifier must FAIL (the negative control). `fail_a2=True` simulates never reaching A2."""

from __future__ import annotations

from typing import Optional

from cl.anchor.backend import AnchorAttestation, AnchorBackend, AnchorError
from cl.canonical.hash_profile import DEFAULT_PROFILE


class MockAnchorBackend(AnchorBackend):
    def __init__(
        self,
        *,
        height: int = 4,
        wrong_root: Optional[bytes] = None,
        fail_a2: bool = False,
        assurance_level: str = "signature_authentic",
    ) -> None:
        self._height = height
        self._wrong_root = wrong_root
        self._fail_a2 = fail_a2
        self._assurance = assurance_level

    def anchor(self, batch_root: bytes) -> AnchorAttestation:
        if self._fail_a2:
            raise AnchorError("simulated: batch never reached A2")
        anchored = self._wrong_root if self._wrong_root is not None else batch_root
        return AnchorAttestation(
            opaque_root=anchored,
            anchored_at_height=self._height,
            finality_certificate_hash=DEFAULT_PROFILE.digest(b"cert:" + anchored),
            assurance_level=self._assurance,
        )
