from __future__ import annotations

"""Data structures describing subscription tiers and their capabilities."""

from dataclasses import dataclass
from enum import Enum
from typing import Dict


class SubscriptionTier(str, Enum):
    """Supported subscription tiers."""

    FREE = "free"
    PRO = "pro"
    ENTERPRISE = "enterprise"
    SYSOP = "sysop"  # Special developer/admin tier with unlimited access


@dataclass(frozen=True)
class SubscriptionFeatures:
    """Feature flags associated with a subscription tier."""

    tier: SubscriptionTier
    domain_limit: int
    max_users: int
    allow_invasive_tests: bool
    allow_html_export: bool
    allow_multi_user: bool
    allow_api_access: bool
    
    # SAST (Static Application Security Testing) permissions
    allow_source_scan: bool = False
    max_source_files: int = 0
    max_source_size_mb: int = 0
    advanced_rules: bool = False


_FEATURE_MATRIX: Dict[SubscriptionTier, SubscriptionFeatures] = {
    SubscriptionTier.FREE: SubscriptionFeatures(
        tier=SubscriptionTier.FREE,
        domain_limit=3,
        max_users=1,
        allow_invasive_tests=False,
        allow_html_export=False,
        allow_multi_user=False,
        allow_api_access=False,
        # SAST not available for FREE tier
        allow_source_scan=False,
        max_source_files=0,
        max_source_size_mb=0,
        advanced_rules=False,
    ),
    SubscriptionTier.PRO: SubscriptionFeatures(
        tier=SubscriptionTier.PRO,
        domain_limit=-1,  # Unlimited
        max_users=5,
        allow_invasive_tests=True,
        allow_html_export=True,
        allow_multi_user=False,
        allow_api_access=False,
        # SAST basic for PRO tier - 100 fichiers par mois
        allow_source_scan=True,
        max_source_files=100,
        max_source_size_mb=20,
        advanced_rules=False,
    ),
    SubscriptionTier.ENTERPRISE: SubscriptionFeatures(
        tier=SubscriptionTier.ENTERPRISE,
        domain_limit=-1,
        max_users=50,
        allow_invasive_tests=True,
        allow_html_export=True,
        allow_multi_user=True,
        allow_api_access=True,
        # SAST advanced for ENTERPRISE tier - 500 fichiers par mois
        allow_source_scan=True,
        max_source_files=500,
        max_source_size_mb=100,
        advanced_rules=True,
    ),
    SubscriptionTier.SYSOP: SubscriptionFeatures(
        tier=SubscriptionTier.SYSOP,
        domain_limit=-1,  # Unlimited
        max_users=-1,     # Unlimited users
        allow_invasive_tests=True,
        allow_html_export=True,
        allow_multi_user=True,
        allow_api_access=True,
        # SAST unlimited for SYSOP tier
        allow_source_scan=True,
        max_source_files=-1,  # Unlimited
        max_source_size_mb=-1,  # Unlimited
        advanced_rules=True,
    ),
}


def get_features_for_tier(tier: SubscriptionTier) -> SubscriptionFeatures:
    """Return the feature set associated with a tier."""
    try:
        return _FEATURE_MATRIX[tier]
    except KeyError as exc:  # pragma: no cover - defensive guard
        raise ValueError(f"Unsupported subscription tier: {tier}") from exc
