"""A CMS that accepts registrations, for tests that are about something else.

Since ADR-0010 a document does not proceed until the CMS has a record of it, so
every test that puts a document through the gate needs a CMS to register it
with. That is deliberate — a fixture that forgets to provide one produces a test
where nothing is processed, which is exactly what a real deployment with an
unreachable CMS does.

This is the "it works" half. Tests about the failure half build their own.
"""

from __future__ import annotations


class RecordingCms:
    """Accepts every registration and remembers it."""

    def __init__(self, start: int = 1000) -> None:
        self.registered: list[dict] = []
        self._next = start

    def register_intake(self, submission, path=None):
        self._next += 1
        self.registered.append(dict(submission))

        return {
            "reference": f"INT-{self._next}",
            "status": "failed" if submission.get("failure_reason") else "received",
        }

    def update_intake(self, reference, **fields):
        return {"reference": reference, **fields}

    @property
    def references(self) -> list[str]:
        return [f"INT-{1000 + n}" for n in range(1, len(self.registered) + 1)]


class UnreachableCms:
    """Refuses everything, the way a CMS that is down does."""

    def __init__(self, error: str = "connection refused") -> None:
        self.error = error
        self.attempts = 0

    def register_intake(self, submission, path=None):
        self.attempts += 1

        raise ConnectionError(self.error)

    def update_intake(self, reference, **fields):
        raise ConnectionError(self.error)
