"""Tier → feature mapping.

A tier unlocks every feature listed for it. Adding a new SKU means
adding a new row here and (if the SKU introduces new functionality)
adding feature flags to :class:`~src.license.schema.FeatureFlag`. No
consumer code changes.

The v1 product ships only :data:`Tier.CORE`, which unlocks every tool.
TRIAL exists so a buyer can register without a paid key and still get
a 1-year working license; the difference between TRIAL and CORE is
semantic (and the basis for showing "TRIAL" in the sidebar), not
functional.

PRO and ENTERPRISE are scaffolded for future SKUs. They currently
unlock the same feature set as CORE so the architecture is exercised
by tests without committing to a particular pricing structure.
"""

from __future__ import annotations

from typing import FrozenSet

from .schema import FeatureFlag, Tier


def _all() -> FrozenSet[FeatureFlag]:
    """Every feature flag — used as the default for the v1 SKU."""
    return frozenset(FeatureFlag)


FEATURES_BY_TIER: dict[Tier, FrozenSet[FeatureFlag]] = {
    # TRIAL is no longer reachable from the activation UI (no free
    # trial in v1.6) but we leave the mapping in place so legacy
    # trial licenses on disk still load. Effectively LITE-equivalent
    # if anyone has one.
    Tier.TRIAL: frozenset({
        FeatureFlag.DEDUPLICATOR,
        FeatureFlag.TEXT_CLEANER,
        FeatureFlag.FORMAT_STANDARDIZER,
    }),
    # LITE — the cheap SKU. Three tools that cover the common
    # bookkeeping / RevOps workflow: clean text, standardise formats,
    # remove duplicates. Buyers who want missing-handler, column-
    # mapper, outlier-detector, validator, or the pipeline runner
    # upgrade to CORE.
    Tier.LITE: frozenset({
        FeatureFlag.DEDUPLICATOR,
        FeatureFlag.TEXT_CLEANER,
        FeatureFlag.FORMAT_STANDARDIZER,
    }),
    # CORE — the v1 full SKU. Every Ready tool.
    Tier.CORE: _all(),
    # Pre-wired for future SKUs. Today they mirror CORE so the gating
    # tests exercise the lookup path without making a marketing claim.
    Tier.PRO: _all(),
    Tier.ENTERPRISE: _all(),
}


def all_features_for_tier(tier: Tier) -> tuple[str, ...]:
    """Return the canonical, sorted tuple of feature ids for *tier*.

    Used by the license generator to fill the ``features`` field on a
    new license, and by the manager to upgrade an older license whose
    ``features`` list omits a flag we've since added to its tier.
    """
    flags = FEATURES_BY_TIER[tier]
    return tuple(sorted(f.value for f in flags))
