"""Pure off-ball movement and space-occupation policy for Match Engine v3.9+.

No Django imports are allowed here.  The module provides a stable football
vocabulary and deterministic policy helpers used by the causal engine,
persistence projection and analytics validation.
"""
from __future__ import annotations

from dataclasses import dataclass

MOVEMENT_TYPES = (
    "SUPPORT_RUN",
    "OVERLAP",
    "UNDERLAP",
    "THIRD_MAN_RUN",
    "HALF_SPACE_OCCUPATION",
    "WIDTH_HOLD",
    "DEPTH_RUN",
    "POSITIONAL_ROTATION",
    "OVERLOAD_CREATED",
)

SPACE_LANES = (
    "LEFT_WIDE",
    "LEFT_HALF_SPACE",
    "CENTRE",
    "RIGHT_HALF_SPACE",
    "RIGHT_WIDE",
)

# Tactical-role tendencies.  These are participation biases, not direct quality
# multipliers; attributes still decide whether the movement is executed well.
_ROLE_MOVEMENT_BIAS = {
    "GOALKEEPER": {"SUPPORT_RUN": 0.05},
    "SWEEPER_KEEPER": {"SUPPORT_RUN": 0.12},
    "CENTRE_BACK": {"SUPPORT_RUN": 0.45, "POSITIONAL_ROTATION": 0.18},
    "BALL_PLAYING_DEFENDER": {"SUPPORT_RUN": 0.72, "POSITIONAL_ROTATION": 0.44},
    "NO_NONSENSE_CENTRE_BACK": {"SUPPORT_RUN": 0.30},
    "FULL_BACK": {"OVERLAP": 1.15, "WIDTH_HOLD": 0.88, "SUPPORT_RUN": 0.72},
    "WING_BACK": {"OVERLAP": 1.35, "WIDTH_HOLD": 1.08, "UNDERLAP": 0.52, "DEPTH_RUN": 0.66},
    "INVERTED_FULL_BACK": {"UNDERLAP": 1.18, "HALF_SPACE_OCCUPATION": 1.16, "POSITIONAL_ROTATION": 0.98, "SUPPORT_RUN": 0.86},
    "ANCHOR": {"SUPPORT_RUN": 0.72, "POSITIONAL_ROTATION": 0.35},
    "BALL_WINNING_MIDFIELDER": {"SUPPORT_RUN": 0.76, "THIRD_MAN_RUN": 0.52, "DEPTH_RUN": 0.40},
    "DEEP_LYING_PLAYMAKER": {"SUPPORT_RUN": 0.95, "POSITIONAL_ROTATION": 0.82, "THIRD_MAN_RUN": 0.62},
    "CENTRAL_MIDFIELDER": {"SUPPORT_RUN": 0.90, "THIRD_MAN_RUN": 0.82, "POSITIONAL_ROTATION": 0.68},
    "BOX_TO_BOX": {"THIRD_MAN_RUN": 1.12, "DEPTH_RUN": 1.05, "SUPPORT_RUN": 0.78, "HALF_SPACE_OCCUPATION": 0.54},
    "ADVANCED_PLAYMAKER": {"HALF_SPACE_OCCUPATION": 1.18, "THIRD_MAN_RUN": 1.02, "POSITIONAL_ROTATION": 0.92, "SUPPORT_RUN": 0.78},
    "WIDE_MIDFIELDER": {"WIDTH_HOLD": 1.08, "OVERLAP": 0.64, "UNDERLAP": 0.48, "SUPPORT_RUN": 0.72},
    "WINGER": {"WIDTH_HOLD": 1.16, "DEPTH_RUN": 0.92, "UNDERLAP": 0.55, "OVERLAP": 0.42},
    "INSIDE_FORWARD": {"HALF_SPACE_OCCUPATION": 1.28, "DEPTH_RUN": 1.10, "UNDERLAP": 0.74, "THIRD_MAN_RUN": 0.62},
    "TARGET_FORWARD": {"SUPPORT_RUN": 0.78, "DEPTH_RUN": 0.58, "THIRD_MAN_RUN": 0.32},
    "PRESSING_FORWARD": {"DEPTH_RUN": 0.94, "SUPPORT_RUN": 0.70, "THIRD_MAN_RUN": 0.52},
    "POACHER": {"DEPTH_RUN": 1.38, "SUPPORT_RUN": 0.28},
    "FALSE_NINE": {"POSITIONAL_ROTATION": 1.24, "HALF_SPACE_OCCUPATION": 1.02, "THIRD_MAN_RUN": 0.88, "SUPPORT_RUN": 0.86},
    "COMPLETE_FORWARD": {"DEPTH_RUN": 1.08, "THIRD_MAN_RUN": 0.70, "SUPPORT_RUN": 0.68, "HALF_SPACE_OCCUPATION": 0.48},
}


@dataclass(frozen=True, slots=True)
class MovementEffect:
    movement_type: str
    receiver_weight_multiplier: float
    completion_delta: float
    press_relief: float
    chance_delta: float
    overload_probability: float


_EFFECTS = {
    "SUPPORT_RUN": MovementEffect("SUPPORT_RUN", 1.12, 0.008, 0.004, 0.003, 0.05),
    "OVERLAP": MovementEffect("OVERLAP", 1.34, 0.015, 0.010, 0.008, 0.34),
    "UNDERLAP": MovementEffect("UNDERLAP", 1.32, 0.017, 0.012, 0.010, 0.31),
    "THIRD_MAN_RUN": MovementEffect("THIRD_MAN_RUN", 1.42, 0.022, 0.015, 0.014, 0.18),
    "HALF_SPACE_OCCUPATION": MovementEffect("HALF_SPACE_OCCUPATION", 1.30, 0.016, 0.011, 0.011, 0.23),
    "WIDTH_HOLD": MovementEffect("WIDTH_HOLD", 1.20, 0.010, 0.007, 0.005, 0.20),
    "DEPTH_RUN": MovementEffect("DEPTH_RUN", 1.44, 0.018, 0.010, 0.016, 0.12),
    "POSITIONAL_ROTATION": MovementEffect("POSITIONAL_ROTATION", 1.24, 0.014, 0.016, 0.009, 0.16),
    "OVERLOAD_CREATED": MovementEffect("OVERLOAD_CREATED", 1.00, 0.008, 0.012, 0.010, 0.0),
}


def movement_bias(role: str, movement_type: str) -> float:
    return float(_ROLE_MOVEMENT_BIAS.get(str(role), {}).get(str(movement_type), 0.12))


def movement_effect(movement_type: str) -> MovementEffect:
    return _EFFECTS.get(str(movement_type), _EFFECTS["SUPPORT_RUN"])


def side_for_slot(slot: str) -> str:
    slot = str(slot or "")
    if slot.startswith("L") or slot in {"LB", "LWB", "LW", "LM"}:
        return "LEFT"
    if slot.startswith("R") or slot in {"RB", "RWB", "RW", "RM"}:
        return "RIGHT"
    return "CENTRE"


def lane_for(*, movement_type: str, slot: str, destination_channel: str) -> str:
    side = side_for_slot(slot)
    channel = str(destination_channel)
    movement_type = str(movement_type)
    if movement_type in {"HALF_SPACE_OCCUPATION", "UNDERLAP"}:
        if side == "LEFT" or channel == "LEFT":
            return "LEFT_HALF_SPACE"
        if side == "RIGHT" or channel == "RIGHT":
            return "RIGHT_HALF_SPACE"
        return "CENTRE"
    if movement_type in {"OVERLAP", "WIDTH_HOLD"}:
        if side == "LEFT" or channel == "LEFT":
            return "LEFT_WIDE"
        if side == "RIGHT" or channel == "RIGHT":
            return "RIGHT_WIDE"
        return "CENTRE"
    if movement_type in {"THIRD_MAN_RUN", "DEPTH_RUN", "POSITIONAL_ROTATION", "SUPPORT_RUN"}:
        if channel == "LEFT" and movement_type == "POSITIONAL_ROTATION":
            return "LEFT_HALF_SPACE"
        if channel == "RIGHT" and movement_type == "POSITIONAL_ROTATION":
            return "RIGHT_HALF_SPACE"
        return "CENTRE" if side == "CENTRE" else ("LEFT_HALF_SPACE" if side == "LEFT" else "RIGHT_HALF_SPACE")
    return "CENTRE"


def strategy_multiplier(attacking_strategy: str, movement_type: str) -> float:
    strategy = str(attacking_strategy)
    move = str(movement_type)
    multipliers = {
        "POSSESSION": {
            "SUPPORT_RUN": 1.18, "THIRD_MAN_RUN": 1.22, "POSITIONAL_ROTATION": 1.30,
            "HALF_SPACE_OCCUPATION": 1.18, "WIDTH_HOLD": 1.05,
        },
        "WIDE_PLAY": {
            "OVERLAP": 1.34, "WIDTH_HOLD": 1.30, "UNDERLAP": 1.12,
            "HALF_SPACE_OCCUPATION": 1.08,
        },
        "DIRECT": {"DEPTH_RUN": 1.24, "THIRD_MAN_RUN": 0.92, "SUPPORT_RUN": 0.88},
        "COUNTER_ATTACK": {"DEPTH_RUN": 1.38, "THIRD_MAN_RUN": 1.12, "OVERLAP": 0.86},
        "HIGH_TEMPO": {"DEPTH_RUN": 1.18, "THIRD_MAN_RUN": 1.12, "OVERLAP": 1.08, "UNDERLAP": 1.08},
        "BALANCED": {},
    }
    return float(multipliers.get(strategy, {}).get(move, 1.0))


def destination_zone_for_movement(movement_type: str, current_zone: str, proposed_zone: str) -> str:
    """Keep movement realistic without inventing a second spatial coordinate system."""
    if movement_type == "DEPTH_RUN" and current_zone == "FINAL_THIRD":
        return "PENALTY_AREA"
    if movement_type in {"THIRD_MAN_RUN", "HALF_SPACE_OCCUPATION", "UNDERLAP"} and current_zone == "MIDDLE_THIRD" and proposed_zone == "MIDDLE_THIRD":
        return "FINAL_THIRD"
    return str(proposed_zone)
