"""Shared constants and helpers for Tarot deck structure and ordering.

This module centralizes deck ordering, pip naming, and position mapping
so other modules can stay DRY and rely on a single source of truth.
"""

from typing import Dict, List, Tuple

ACE_NAME = "Ace"

# Minor sequencing and naming
# Ace is its own category (container for the suit), pips are 2-10, then face cards
PIP_ORDER: List[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

# Numeric pip names only (2-10). Ace is intentionally excluded because it is not a pip card.
PIP_NAMES: Dict[int, str] = {
    2: "Two",
    3: "Three",
    4: "Four",
    5: "Five",
    6: "Six",
    7: "Seven",
    8: "Eight",
    9: "Nine",
    10: "Ten",
}

# Full minor rank names (Ace, 2-10, then courts)
MINOR_RANK_NAMES: Dict[int, str] = {
    1: ACE_NAME,
    **PIP_NAMES,
    11: "Prince",
    12: "Knight",
    13: "Princess",
    14: "Queen",
}

# Map pip-order indices to actual pip numbers (1 maps to Ace, 2-10 are pip cards)
PIP_INDEX_TO_NUMBER: Dict[int, int] = {
    1: 1,
    2: 2,
    3: 3,
    4: 4,
    5: 5,
    6: 6,
    7: 7,
    8: 8,
    9: 9,
    10: 10,
}

COURT_RANKS: Dict[int, str] = {
    11: "Prince",
    12: "Knight",
    13: "Princess",
    14: "Queen",
}

# Suit sequencing for the deck (number matches Suit.number)
SUITS_FIRST: List[Tuple[str, str, int]] = [
    ("Cups", "Water", 2),
    ("Pentacles", "Earth", 4),
    ("Swords", "Air", 3),
]
SUITS_LAST: List[Tuple[str, str, int]] = [
    ("Wands", "Fire", 1),
]

MAJOR_ARCANA_NAMES: List[str] = [
    "Fool",
    "Magus",
    "Fortune",
    "Lust",
    "Hanged Man",
    "Death",
    "Art",
    "Devil",
    "Tower",
    "Star",
    "Moon",
    "Sun",
    "High Priestess",
    "Empress",
    "Emperor",
    "Hierophant",
    "Lovers",
    "Chariot",
    "Justice",
    "Hermit",
    "Aeon",
    "Universe",
]

MAJOR_ARCANA_KEYS: List[str] = [
    "o",
    "I",
    "II",
    "III",
    "IV",
    "V",
    "VI",
    "VII",
    "VIII",
    "IX",
    "X",
    "XI",
    "XII",
    "XIII",
    "XIV",
    "XV",
    "XVI",
    "XVII",
    "XVIII",
    "XIX",
    "XX",
    "XXI",
]


def minor_card_names_for_suit(suit_name: str) -> List[str]:
    """Return the ordered card names for a minor suit using the shared rank order."""
    return [f"{MINOR_RANK_NAMES[pip_index]} of {suit_name}" for pip_index in PIP_ORDER]


def build_position_map() -> Dict[int, str]:
    """Build the position map (1-78) aligned to deck ordering."""
    position_map: Dict[int, str] = {}
    pos = 1

    # Cups, Pentacles, Swords (1-42)
    for suit_name, _, _ in SUITS_FIRST:
        for name in minor_card_names_for_suit(suit_name):
            position_map[pos] = name
            pos += 1

    # Major Arcana (43-64)
    for key in MAJOR_ARCANA_KEYS:
        position_map[pos] = key
        pos += 1

    # Wands (65-78)
    for suit_name, _, _ in SUITS_LAST:
        for name in minor_card_names_for_suit(suit_name):
            position_map[pos] = name
            pos += 1

    return position_map


__all__ = [
    "ACE_NAME",
    "PIP_ORDER",
    "PIP_NAMES",
    "MINOR_RANK_NAMES",
    "PIP_INDEX_TO_NUMBER",
    "COURT_RANKS",
    "SUITS_FIRST",
    "SUITS_LAST",
    "MAJOR_ARCANA_NAMES",
    "MAJOR_ARCANA_KEYS",
    "minor_card_names_for_suit",
    "build_position_map",
]
