"""Versioned balancing configuration for the first FTG match engine.

This module is intentionally pure Python.  The numbers here are tuning data,
not persistence/domain state, and can evolve under a new engine version without
rewriting historical match snapshots.
"""

from __future__ import annotations


ENGINE_KEY = "ftg.match.v1"
ENGINE_VERSION = "1.0.0"

BASE_EXPECTED_GOALS = 1.18
HOME_ADVANTAGE_XG = 0.18
ATTACK_DEFENCE_XG_FACTOR = 0.026
MIDFIELD_CONTROL_XG_FACTOR = 0.008
MIN_EXPECTED_GOALS = 0.15
MAX_EXPECTED_GOALS = 4.50

# Captain leadership only nudges the team profile.  It must never overwhelm
# the actual football attributes of the starting XI.
LEADERSHIP_REFERENCE = 50.0
LEADERSHIP_FACTOR_PER_POINT = 0.0005

FORMATION_MODIFIERS = {
    "4-3-3": {
        "attack": 1.03,
        "midfield": 1.00,
        "defence": 0.99,
    },
    "4-4-2": {
        "attack": 0.99,
        "midfield": 1.03,
        "defence": 1.01,
    },
}

SLOT_ROLE = {
    "GK": "goalkeeper",
    "LB": "fullback",
    "LCB": "centre_back",
    "RCB": "centre_back",
    "RB": "fullback",
    "LCM": "central_midfield",
    "CM": "central_midfield",
    "RCM": "central_midfield",
    "LM": "wide_midfield",
    "RM": "wide_midfield",
    "LW": "winger",
    "RW": "winger",
    "ST": "striker",
    "LST": "striker",
    "RST": "striker",
}

ROLE_ATTRIBUTE_WEIGHTS = {
    "goalkeeper": {
        "gk_reflexes": 0.23,
        "gk_positioning": 0.18,
        "gk_diving": 0.18,
        "gk_one_on_one": 0.15,
        "gk_handling": 0.12,
        "gk_kicking": 0.06,
        "concentration": 0.04,
        "decision_making": 0.04,
    },
    "centre_back": {
        "marking": 0.18,
        "tackling": 0.18,
        "positioning": 0.14,
        "anticipation": 0.11,
        "heading": 0.09,
        "strength": 0.08,
        "concentration": 0.08,
        "composure": 0.05,
        "pace": 0.04,
        "passing": 0.03,
        "teamwork": 0.02,
    },
    "fullback": {
        "tackling": 0.13,
        "marking": 0.12,
        "positioning": 0.10,
        "pace": 0.11,
        "acceleration": 0.08,
        "stamina": 0.09,
        "crossing": 0.08,
        "passing": 0.07,
        "work_rate": 0.08,
        "anticipation": 0.06,
        "teamwork": 0.04,
        "ball_control": 0.04,
    },
    "central_midfield": {
        "passing": 0.16,
        "vision": 0.14,
        "decision_making": 0.12,
        "ball_control": 0.11,
        "teamwork": 0.09,
        "stamina": 0.08,
        "first_touch": 0.08,
        "composure": 0.06,
        "work_rate": 0.06,
        "positioning": 0.05,
        "anticipation": 0.05,
    },
    "wide_midfield": {
        "crossing": 0.15,
        "passing": 0.12,
        "ball_control": 0.10,
        "dribbling": 0.10,
        "pace": 0.10,
        "stamina": 0.09,
        "work_rate": 0.09,
        "vision": 0.08,
        "acceleration": 0.06,
        "teamwork": 0.06,
        "decision_making": 0.05,
    },
    "winger": {
        "dribbling": 0.15,
        "pace": 0.12,
        "ball_control": 0.11,
        "crossing": 0.11,
        "finishing": 0.10,
        "off_ball": 0.09,
        "acceleration": 0.08,
        "first_touch": 0.07,
        "composure": 0.06,
        "decision_making": 0.05,
        "vision": 0.04,
        "shooting": 0.02,
    },
    "striker": {
        "finishing": 0.20,
        "positioning": 0.15,
        "composure": 0.12,
        "off_ball": 0.10,
        "shooting": 0.10,
        "first_touch": 0.08,
        "ball_control": 0.07,
        "heading": 0.06,
        "strength": 0.05,
        "anticipation": 0.04,
        "decision_making": 0.03,
    },
}

# Natural-position suitability for each tactical slot family.  Out-of-position
# play is allowed by the lineup foundation, but its effect is versioned here.
POSITION_FIT = {
    "goalkeeper": {
        "GK": 1.00,
    },
    "centre_back": {
        "CB": 1.00,
        "CDM": 0.90,
        "LB": 0.88,
        "RB": 0.88,
        "CM": 0.78,
    },
    "fullback": {
        "LB": 1.00,
        "RB": 1.00,
        "LM": 0.88,
        "RM": 0.88,
        "CB": 0.86,
        "LW": 0.84,
        "RW": 0.84,
        "CDM": 0.82,
        "CM": 0.80,
    },
    "central_midfield": {
        "CM": 1.00,
        "CDM": 0.96,
        "CAM": 0.96,
        "LM": 0.90,
        "RM": 0.90,
        "CF": 0.82,
        "LW": 0.80,
        "RW": 0.80,
    },
    "wide_midfield": {
        "LM": 1.00,
        "RM": 1.00,
        "LW": 0.96,
        "RW": 0.96,
        "CM": 0.88,
        "CAM": 0.88,
        "LB": 0.88,
        "RB": 0.88,
        "ST": 0.78,
        "CF": 0.78,
    },
    "winger": {
        "LW": 1.00,
        "RW": 1.00,
        "LM": 0.95,
        "RM": 0.95,
        "CAM": 0.88,
        "CF": 0.88,
        "ST": 0.84,
        "LB": 0.80,
        "RB": 0.80,
    },
    "striker": {
        "ST": 1.00,
        "CF": 1.00,
        "CAM": 0.90,
        "LW": 0.88,
        "RW": 0.88,
        "LM": 0.80,
        "RM": 0.80,
        "CM": 0.76,
    },
}

DEFAULT_OUT_OF_POSITION_FIT = 0.70

CAPTAIN_LEADERSHIP_ATTRIBUTES = (
    "teamwork",
    "decision_making",
    "composure",
    "concentration",
)

DEFENDER_ROLES = frozenset({"centre_back", "fullback"})
MIDFIELD_ROLES = frozenset({"central_midfield", "wide_midfield"})
FORWARD_ROLES = frozenset({"winger", "striker"})

# Unit blend intentionally sums to one.  It keeps the first engine explainable
# and allows later versions to rebalance without changing the snapshot schema.
ATTACK_UNIT_WEIGHTS = {
    "forwards": 0.67,
    "midfield": 0.25,
    "defenders": 0.08,
}
MIDFIELD_UNIT_WEIGHTS = {
    "midfield": 0.72,
    "defenders": 0.16,
    "forwards": 0.12,
}
DEFENCE_UNIT_WEIGHTS = {
    "defenders": 0.72,
    "midfield": 0.22,
    "forwards": 0.06,
}
