"""GameGrip Safety API — Configuration & Policy Packs"""

from enum import Enum
from typing import Dict, List, Optional
from pydantic import BaseModel, Field


class AgeBand(str, Enum):
    UNDER_13 = "under_13"
    TEEN_13_15 = "13-15"
    TEEN_16_17 = "16-17"
    ADULT_18_PLUS = "18+"


class Platform(str, Enum):
    LOBBY = "lobby"
    DM = "dm"
    TRADE = "trade"
    GUILD = "guild"
    GENERAL = "general"


class Decision(str, Enum):
    ALLOW = "allow"
    WARN = "warn"
    BLOCK = "block"
    REVIEW = "review"


# ───────────────────────────────────────────────
# Policy Pack System (Phase 3)
# ───────────────────────────────────────────────
class PolicyThresholds(BaseModel):
    """Per-policy decision thresholds."""
    allow_max: int = Field(30, ge=0, le=100)
    warn_max: int = Field(70, ge=0, le=100)
    # block is everything above warn_max


class PolicyConfig(BaseModel):
    """Full configuration for a policy pack."""
    name: str
    description: str
    thresholds: PolicyThresholds
    age_boosts: Dict[AgeBand, int]
    platform_multipliers: Dict[Platform, float]
    enabled_categories: List[str]  # which detectors run
    escalation_rules: Dict[str, str]  # category -> action override
    log_level: str = "standard"  # standard | verbose | audit
    require_review_above: Optional[int] = None  # auto-escalate to review queue


# ───────────────────────────────────────────────
# Built-in Policy Packs
# ───────────────────────────────────────────────
POLICY_PACKS: Dict[str, PolicyConfig] = {
    "child_strict": PolicyConfig(
        name="child_strict",
        description="Maximum protection for users under 13. Aggressive blocking, zero tolerance.",
        thresholds=PolicyThresholds(allow_max=20, warn_max=50),
        age_boosts={
            AgeBand.UNDER_13: 35,
            AgeBand.TEEN_13_15: 25,
            AgeBand.TEEN_16_17: 15,
            AgeBand.ADULT_18_PLUS: 0,
        },
        platform_multipliers={
            Platform.TRADE: 1.3,
            Platform.DM: 1.2,
            Platform.LOBBY: 1.0,
            Platform.GUILD: 1.0,
            Platform.GENERAL: 1.0,
        },
        enabled_categories=["toxicity", "sexual", "scam", "grooming", "harassment"],
        escalation_rules={"sexual": "block", "scam": "block", "grooming": "block"},
        log_level="audit",
        require_review_above=80,
    ),
    "teen_safe": PolicyConfig(
        name="teen_safe",
        description="Balanced protection for teens. Warn-first approach with education.",
        thresholds=PolicyThresholds(allow_max=30, warn_max=65),
        age_boosts={
            AgeBand.UNDER_13: 30,
            AgeBand.TEEN_13_15: 20,
            AgeBand.TEEN_16_17: 10,
            AgeBand.ADULT_18_PLUS: 0,
        },
        platform_multipliers={
            Platform.TRADE: 1.25,
            Platform.DM: 1.15,
            Platform.LOBBY: 1.0,
            Platform.GUILD: 1.0,
            Platform.GENERAL: 1.0,
        },
        enabled_categories=["toxicity", "sexual", "scam", "grooming", "harassment"],
        escalation_rules={"grooming": "block", "sexual": "warn"},
        log_level="verbose",
        require_review_above=85,
    ),
    "general_community": PolicyConfig(
        name="general_community",
        description="Standard community moderation. Focus on illegal and extreme content.",
        thresholds=PolicyThresholds(allow_max=35, warn_max=75),
        age_boosts={
            AgeBand.UNDER_13: 25,
            AgeBand.TEEN_13_15: 15,
            AgeBand.TEEN_16_17: 5,
            AgeBand.ADULT_18_PLUS: 0,
        },
        platform_multipliers={
            Platform.TRADE: 1.15,
            Platform.DM: 1.1,
            Platform.LOBBY: 1.0,
            Platform.GUILD: 1.0,
            Platform.GENERAL: 1.0,
        },
        enabled_categories=["toxicity", "sexual", "scam", "grooming", "harassment"],
        escalation_rules={"grooming": "block"},
        log_level="standard",
        require_review_above=90,
    ),
    "competitive_game_mode": PolicyConfig(
        name="competitive_game_mode",
        description="Relaxed toxicity for competitive contexts. Block only severe content.",
        thresholds=PolicyThresholds(allow_max=45, warn_max=80),
        age_boosts={
            AgeBand.UNDER_13: 20,
            AgeBand.TEEN_13_15: 10,
            AgeBand.TEEN_16_17: 5,
            AgeBand.ADULT_18_PLUS: 0,
        },
        platform_multipliers={
            Platform.TRADE: 1.2,
            Platform.DM: 1.1,
            Platform.LOBBY: 1.0,
            Platform.GUILD: 1.0,
            Platform.GENERAL: 1.0,
        },
        enabled_categories=["toxicity", "sexual", "scam", "grooming", "harassment"],
        escalation_rules={"sexual": "block", "grooming": "block"},
        log_level="standard",
        require_review_above=95,
    ),
}


# ───────────────────────────────────────────────
# Global Settings
# ───────────────────────────────────────────────
class Settings(BaseModel):
    app_name: str = "GameGrip Safety API"
    version: str = "1.3.0"
    default_policy: str = "general_community"
    conversation_history_limit: int = 10
    enable_review_queue: bool = True
    enable_admin_override: bool = True


settings = Settings()
