"""Pure pressing/defensive-shape primitives for FTG Match Engine v3.8+.

This module intentionally has no Django imports.  Defensive shape is derived
from the already-frozen tactical strategy so historical kickoff snapshots remain
self-contained while the causal engine can reason about line height,
compactness, width, press intensity and counter-press intent.
"""
from __future__ import annotations

from dataclasses import dataclass

PRESSING_TRIGGERS = (
    "BUILDUP_RECEIPT",
    "BACK_PASS",
    "WIDE_TRAP",
    "CENTRAL_COMPACTNESS",
    "HEAVY_TOUCH",
    "PROGRESSIVE_PASS",
    "TURNOVER",
)

PRESSING_EVENT_TYPES = (
    "PRESS_TRIGGERED",
    "PRESS_RECOVERY",
    "PRESS_BYPASSED",
    "PRESSING_TRAP",
    "LINE_BREAK",
    "COUNTER_PRESS",
    "COUNTER_PRESS_RECOVERY",
    "COUNTER_PRESS_BYPASSED",
)


@dataclass(frozen=True, slots=True)
class DefensiveShapeProfile:
    strategy: str
    line_height: int
    compactness: int
    width: int
    press_intensity: float
    counterpress_intensity: float
    trap_intensity: float

    def to_payload(self) -> dict:
        return {
            "strategy": self.strategy,
            "line_height": self.line_height,
            "compactness": self.compactness,
            "width": self.width,
            "press_intensity": round(self.press_intensity, 3),
            "counterpress_intensity": round(self.counterpress_intensity, 3),
            "trap_intensity": round(self.trap_intensity, 3),
        }


_PROFILES = {
    "BALANCED": DefensiveShapeProfile("BALANCED", 52, 68, 68, 0.43, 0.42, 0.36),
    "HIGH_PRESS": DefensiveShapeProfile("HIGH_PRESS", 82, 64, 62, 0.82, 0.78, 0.64),
    "MID_BLOCK": DefensiveShapeProfile("MID_BLOCK", 58, 76, 66, 0.55, 0.47, 0.52),
    "LOW_BLOCK": DefensiveShapeProfile("LOW_BLOCK", 34, 86, 72, 0.28, 0.22, 0.34),
    "MAN_MARKING": DefensiveShapeProfile("MAN_MARKING", 63, 58, 70, 0.61, 0.55, 0.46),
    "ZONAL_COMPACT": DefensiveShapeProfile("ZONAL_COMPACT", 48, 90, 56, 0.50, 0.50, 0.58),
}


def shape_for_strategy(strategy: str) -> DefensiveShapeProfile:
    return _PROFILES.get(str(strategy), _PROFILES["BALANCED"])


def zone_press_factor(strategy: str, zone: str) -> float:
    strategy = str(strategy)
    zone = str(zone)
    by_strategy = {
        "HIGH_PRESS": {"DEFENSIVE_THIRD": 1.24, "MIDDLE_THIRD": 1.14, "FINAL_THIRD": 0.90, "PENALTY_AREA": 0.72},
        "MID_BLOCK": {"DEFENSIVE_THIRD": 0.72, "MIDDLE_THIRD": 1.18, "FINAL_THIRD": 1.02, "PENALTY_AREA": 0.86},
        "LOW_BLOCK": {"DEFENSIVE_THIRD": 0.45, "MIDDLE_THIRD": 0.70, "FINAL_THIRD": 1.12, "PENALTY_AREA": 1.28},
        "MAN_MARKING": {"DEFENSIVE_THIRD": 0.92, "MIDDLE_THIRD": 1.06, "FINAL_THIRD": 1.10, "PENALTY_AREA": 1.05},
        "ZONAL_COMPACT": {"DEFENSIVE_THIRD": 0.76, "MIDDLE_THIRD": 1.02, "FINAL_THIRD": 1.16, "PENALTY_AREA": 1.22},
        "BALANCED": {"DEFENSIVE_THIRD": 0.82, "MIDDLE_THIRD": 1.00, "FINAL_THIRD": 1.04, "PENALTY_AREA": 1.00},
    }
    return by_strategy.get(strategy, by_strategy["BALANCED"]).get(zone, 1.0)


def compactness_pass_delta(*, strategy: str, destination_zone: str, destination_channel: str, pass_type: str) -> float:
    """Return completion-probability delta caused only by defensive geometry.

    Compact central blocks close through-ball lanes, while very narrow shapes
    naturally concede a little more room to clean switches into wide channels.
    High lines are intentionally vulnerable to a well-executed through ball.
    """
    profile = shape_for_strategy(strategy)
    delta = 0.0
    if destination_channel == "CENTRE" and destination_zone in {"FINAL_THIRD", "PENALTY_AREA"}:
        delta -= max(0, profile.compactness - 60) * 0.00042
    if pass_type == "THROUGH_BALL":
        delta += max(0, profile.line_height - 60) * 0.00038
        delta -= max(0, profile.compactness - 72) * 0.00020
    if pass_type == "SWITCH" and destination_channel in {"LEFT", "RIGHT"}:
        delta += max(0, 68 - profile.width) * 0.00032
    return max(-0.055, min(0.040, delta))
