"""
Synthetic Discord client backed by a JSON fixture.

Used only by ``--from-fixture`` repro mode: ``build_plan`` accepts
this in place of an ``AuditedDiscordClient`` so an operator can
replay classification logic against a snapshot attached to a
ticket. Lives here (not in the main client module) because it is
a CLI-only concern and the protocol it implements is implicit
(``guild_roles`` + ``guild_members`` are the only methods
``build_plan`` reaches for).
"""

from __future__ import annotations

from typing import TYPE_CHECKING, cast

from ._stubs import _RoleStub

if TYPE_CHECKING:
    from collections.abc import Generator


class _FixtureClient:
    """Synthetic AuditedDiscordClient backed by a JSON fixture."""

    def __init__(
        self,
        roles: list[dict[str, object]],
        members_pages: list[list[dict[str, object]]],
    ) -> None:
        # Roles arrive as JSON dicts; convert to the .id/.name shape
        # build_plan + role_filter consume.  Pages stay as raw dicts —
        # GuildMember.from_dict reads them via bracket access at the
        # classify call site, so re-shaping here would be wasted work.
        # JSON values land as ``object`` in the type system; ``int(...)``
        # rejects ``object`` directly. Cast each field at the parse
        # boundary — fixture authoring trusts caller-provided JSON to
        # be well-formed, and the runtime ``int()`` / ``str()`` calls
        # raise loudly on mistyped values.
        self._roles = [
            _RoleStub(
                role_id=int(cast("int | str", r["id"])),
                name=str(r["name"]),
            )
            for r in roles
        ]
        self._members_pages = members_pages

    def guild_roles(self, guild_id: object) -> list[_RoleStub]:
        del guild_id
        return list(self._roles)

    def guild_members(
        self, *, guild_id: object, limit: int
    ) -> Generator[list[dict[str, object]], None, None]:
        del guild_id, limit
        for page in self._members_pages:
            yield list(page)
