"""Canonical shopping-facet definitions shared by listings, exports, and Magento bootstrap."""

from __future__ import annotations

from typing import Dict, List, Optional

from channel.url_canonical import slugify_segment

# Matches DigitalSilk\Catalog\Setup\Patch\Data\CreateCabinetTypeAttributes option labels.
KITCHEN_CABINET_SHOPPING_OPTIONS: List[Dict[str, str]] = [
    {"label": "Base Cabinets", "slug": "base-cabinets", "magento_label": "base cabinets"},
    {"label": "Wall Cabinets", "slug": "wall-cabinets", "magento_label": "wall cabinets"},
    {"label": "Panels and Fillers", "slug": "panels-and-fillers", "magento_label": "panels and fillers"},
    {"label": "Mouldings", "slug": "mouldings", "magento_label": "mouldings"},
    {"label": "Accessories", "slug": "accessories", "magento_label": "accessories"},
]

HUB_SHOPPING_OPTION_DEFAULTS: Dict[str, List[Dict[str, str]]] = {
    "kitchen-cabinets": KITCHEN_CABINET_SHOPPING_OPTIONS,
}

KITCHEN_CABINET_DETAIL_OPTIONS: Dict[str, List[Dict[str, str]]] = {
    "Wall Cabinets": [
        {"label": "Basic Wall", "slug": "basic-wall", "magento_label": "basic wall"},
        {"label": "Stacked Wall", "slug": "stacked-wall", "magento_label": "stacked wall"},
        {"label": "Bridge Wall", "slug": "bridge-wall", "magento_label": "bridge wall"},
        {"label": "Corner Wall", "slug": "corner-wall", "magento_label": "corner wall"},
        {"label": "Special Wall", "slug": "special-wall", "magento_label": "special wall"},
    ],
    "Base Cabinets": [
        {"label": "Basic Base", "slug": "basic-base", "magento_label": "basic base"},
        {"label": "Drawer Base", "slug": "drawer-base", "magento_label": "drawer base"},
        {"label": "Sink Base", "slug": "sink-base", "magento_label": "sink base"},
        {"label": "Corner Base", "slug": "corner-base", "magento_label": "corner base"},
        {"label": "Special Base", "slug": "special-base", "magento_label": "special base"},
    ],
    "Tall Cabinets": [
        {"label": "Pantry Tall", "slug": "pantry-tall", "magento_label": "pantry tall"},
        {"label": "Oven Tall", "slug": "oven-tall", "magento_label": "oven tall"},
        {"label": "Special Tall", "slug": "special-tall", "magento_label": "special tall"},
    ],
}

COLLECTION_AVAILABILITY_OPTIONS: List[Dict[str, str]] = [
    {
        "label": "Grab & Go",
        "slug": "grab-and-go",
        "value": "grab_and_go",
        "description": "Available immediately for pickup at selected store",
    },
    {"label": "Available", "slug": "available", "value": "available"},
    {"label": "Special Order", "slug": "special-order", "value": "special_order"},
]


def compact_filter_value(label: str) -> str:
    """URL/query filter token, e.g. Wall Cabinets -> wallcabinets."""
    return slugify_segment(label).replace("-", "")


def _norm_label(value: str) -> str:
    return " ".join(str(value or "").strip().lower().split())


def magento_option_label(label: str) -> str:
    """Map master/display labels to Magento cabinet_type option admin labels."""
    norm = _norm_label(label)
    short_aliases = {
        "base": "base cabinets",
        "wall": "wall cabinets",
        "panel": "panels and fillers",
        "panels": "panels and fillers",
        "filler": "panels and fillers",
        "moulding": "mouldings",
        "molding": "mouldings",
        "accessory": "accessories",
        "accessories": "accessories",
        "storage accessories": "accessories",
    }
    if norm in short_aliases:
        return short_aliases[norm]
    for item in KITCHEN_CABINET_SHOPPING_OPTIONS:
        candidates = {
            _norm_label(item["label"]),
            _norm_label(item["magento_label"]),
            _norm_label(item["slug"].replace("-", " ")),
            compact_filter_value(item["label"]),
        }
        if norm in candidates:
            return str(item["magento_label"])
    return norm


def canonical_cabinet_type_label(label: str) -> Optional[str]:
    """Customer-facing label (title case) for master catalog cabinet_type."""
    norm = _norm_label(label)
    for item in KITCHEN_CABINET_SHOPPING_OPTIONS:
        candidates = {
            _norm_label(item["label"]),
            _norm_label(item["magento_label"]),
            compact_filter_value(item["label"]),
        }
        if norm in candidates:
            return str(item["label"])
    return None


def cabinet_type_filter_aliases() -> Dict[str, List[str]]:
    """Extra normalized labels that should resolve to a Magento cabinet_type option."""
    out: Dict[str, List[str]] = {}
    for item in KITCHEN_CABINET_SHOPPING_OPTIONS:
        magento_label = str(item["magento_label"])
        slug = compact_filter_value(item["label"])
        out.setdefault(magento_label, []).extend(
            [
                _norm_label(item["label"]),
                slug,
                item["slug"].replace("-", ""),
                item["slug"],
            ]
        )
    return out
