"""Tarot letter namespace - fluent query interface."""

from typing import TYPE_CHECKING

from utils.query import CollectionAccessor

if TYPE_CHECKING:
    from tarot.card.data import CardDataLoader


class Letter:
    """Fluent query accessor for letters, alphabets, ciphers, and correspondences."""

    def __init__(self) -> None:
        self._initialized: bool = False
        self._loader: "CardDataLoader | None" = None
        self.alphabet = CollectionAccessor(self._get_alphabets)
        self.cipher = CollectionAccessor(self._get_ciphers)
        self.letter = CollectionAccessor(self._get_letters)
        self.iching = CollectionAccessor(self._get_hexagrams)
        self.periodic = CollectionAccessor(self._get_periodic)

    def _ensure_initialized(self) -> None:
        """Lazy-load data from CardDataLoader on first access."""
        if self._initialized:
            return

        from tarot.card.data import CardDataLoader

        self._loader = CardDataLoader()
        self._initialized = True

    def _require_loader(self) -> "CardDataLoader":
        self._ensure_initialized()
        assert self._loader is not None, "Loader not initialized"
        return self._loader

    def _get_alphabets(self):
        loader = self._require_loader()
        return loader._alphabets.copy()

    def _get_ciphers(self):
        loader = self._require_loader()
        return loader._ciphers.copy()

    def _get_letters(self):
        loader = self._require_loader()
        return dict(loader.letter())

    def _get_hexagrams(self):
        loader = self._require_loader()
        return loader._hexagrams.copy()

    def _get_periodic(self):
        loader = self._require_loader()
        return loader._periodic_table.copy()

    def word(self, text: str, *, alphabet: str = "english"):
        """
        Start a fluent cipher request for the given text.

        Usage:
            letter.word('MAGICK').cipher('english_simple')
            letter.word('MAGICK', alphabet='hebrew').cipher('hebrew_standard')
        """
        loader = self._require_loader()
        return loader.word(text, alphabet=alphabet)

    def __str__(self) -> str:
        """Return a nice summary of the letter accessor."""
        return (
            "Letter Namespace - Alphabets, Letters, Ciphers, I Ching, Periodic Table, Words\n\n"
            "Access methods:\n"
            "  letter.alphabet           - English, Hebrew, Greek alphabets\n"
            "  letter.cipher             - Cipher systems (English simple, Hebrew, etc.)\n"
            "  letter.letter             - Hebrew letters (Aleph through Tau)\n"
            "  letter.word(text)         - Encode text with cipher systems\n"
            "  letter.iching             - I Ching trigrams and hexagrams\n"
            "  letter.periodic           - Periodic table with Sephiroth"
        )

    def __repr__(self) -> str:
        """Return a nice representation of the letter accessor."""
        return self.__str__()


# Create singleton instance
letter = Letter()
