"""FIGHT v0 local policy: TEMPORARY_DEMO_HEURISTIC, not action recognition."""
from dataclasses import asdict, dataclass, fields
from math import isfinite


@dataclass(frozen=True)
class FightConfig:
    enabled: bool = False
    person_score: float = 0.25
    joint_score: float = 0.15
    pair_distance_ratio: float = 1.2
    pair_close_hold_seconds: float = 0.5
    arm_motion_window_seconds: float = 0.4
    arm_motion_ratio: float = 0.30
    toward_opponent_ratio: float = 0.20
    fight_window_seconds: float = 1.5
    required_bursts: int = 2
    burst_min_interval_seconds: float = 0.25
    rearm_quiet_seconds: float = 2.0
    pair_retention_seconds: float = 5.0
    max_people: int = 32
    max_pairs: int = 128
    history_capacity: int = 64

    def __post_init__(self):
        if type(self.enabled) is not bool:
            raise ValueError("Invalid FIGHT enabled toggle.")
        integers = {"required_bursts": (1, 16), "max_people": (2, 128),
                    "max_pairs": (1, 2048), "history_capacity": (4, 512)}
        for field in fields(self):
            name, value = field.name, getattr(self, field.name)
            if name == "enabled":
                continue
            if name in integers:
                low, high = integers[name]
                if type(value) is not int or not low <= value <= high:
                    raise ValueError(f"Invalid FIGHT {name}.")
            elif (type(value) not in (int, float) or not isfinite(value)
                  or not (0 <= value <= 1 if name.endswith("_score") else 0 < value <= 30)):
                raise ValueError(f"Invalid FIGHT {name}.")
        if (self.arm_motion_window_seconds > self.fight_window_seconds
                or self.burst_min_interval_seconds > self.fight_window_seconds
                or self.rearm_quiet_seconds < self.fight_window_seconds
                or self.pair_retention_seconds < self.rearm_quiet_seconds):
            raise ValueError("Inconsistent FIGHT windows.")

    @classmethod
    def parse(cls, value):
        if type(value) is not dict or set(value) - {f.name for f in fields(cls)}:
            raise ValueError("Invalid FIGHT local settings.")
        return cls(**value)

    def payload(self):
        """Local config only; never append this to a public Event."""
        return asdict(self)
