"""Pure spatial-possession primitives for FTG Match Engine v3.7+.

The module intentionally contains no Django imports.  It defines the small,
stable vocabulary used by the causal engine, persistence projection and APIs.
"""
from __future__ import annotations

from dataclasses import dataclass

ZONES = ("DEFENSIVE_THIRD", "MIDDLE_THIRD", "FINAL_THIRD", "PENALTY_AREA")
CHANNELS = ("LEFT", "CENTRE", "RIGHT")
TRANSITION_STATES = ("SETTLED", "BUILDUP", "PROGRESSION", "COUNTER", "SECOND_BALL")
PASS_TYPES = ("SHORT", "PROGRESSIVE", "SWITCH", "THROUGH_BALL", "CROSS", "RECYCLE")

_ZONE_INDEX = {zone: index for index, zone in enumerate(ZONES)}


def zone_index(zone: str) -> int:
    return _ZONE_INDEX.get(str(zone), 0)


def is_progressive(origin_zone: str, destination_zone: str) -> bool:
    return zone_index(destination_zone) > zone_index(origin_zone)


def zone_distance(origin_zone: str, destination_zone: str) -> int:
    return abs(zone_index(destination_zone) - zone_index(origin_zone))


@dataclass(frozen=True, slots=True)
class TransitionContext:
    state: str = "SETTLED"
    source_zone: str = "MIDDLE_THIRD"
    expires_minute: int = 0
    caused_by_player_id: str | None = None

    def active(self, minute: int) -> bool:
        return self.state != "SETTLED" and int(minute) <= int(self.expires_minute)
