from __future__ import annotations
from dataclasses import dataclass

from ..state import ATTACKING_STRATEGIES, DEFENSIVE_STRATEGIES, EngineStateError

COMMAND_TACTICS = "TACTICS"
COMMAND_SUBSTITUTION = "SUBSTITUTION"
SUPPORTED_COMMANDS = {COMMAND_TACTICS, COMMAND_SUBSTITUTION}


@dataclass(frozen=True, slots=True)
class LiveCommandState:
    sequence: int
    command_ref: str
    club_id: str
    command_type: str
    effective_segment: int
    attacking_strategy: str | None = None
    defensive_strategy: str | None = None
    player_out_id: str | None = None
    player_in_id: str | None = None

    def __post_init__(self):
        if isinstance(self.sequence, bool) or not isinstance(self.sequence, int) or self.sequence < 1:
            raise EngineStateError("Live command sequence must be a positive integer.")
        if not isinstance(self.command_ref, str) or not self.command_ref.strip():
            raise EngineStateError("Live command_ref must be non-empty.")
        if not isinstance(self.club_id, str) or not self.club_id.strip():
            raise EngineStateError("Live command club_id must be non-empty.")
        if self.command_type not in SUPPORTED_COMMANDS:
            raise EngineStateError("Unsupported live command type.")
        if isinstance(self.effective_segment, bool) or not isinstance(self.effective_segment, int) or not 1 <= self.effective_segment <= 8:
            raise EngineStateError("Live command effective_segment must be 1..8.")
        if self.command_type == COMMAND_TACTICS:
            if self.attacking_strategy not in ATTACKING_STRATEGIES:
                raise EngineStateError("Unsupported live attacking strategy.")
            if self.defensive_strategy not in DEFENSIVE_STRATEGIES:
                raise EngineStateError("Unsupported live defensive strategy.")
            if self.player_out_id is not None or self.player_in_id is not None:
                raise EngineStateError("Tactical live command cannot contain substitution players.")
        else:
            if not isinstance(self.player_out_id, str) or not self.player_out_id.strip():
                raise EngineStateError("Substitution command requires player_out_id.")
            if not isinstance(self.player_in_id, str) or not self.player_in_id.strip():
                raise EngineStateError("Substitution command requires player_in_id.")
            if self.player_out_id == self.player_in_id:
                raise EngineStateError("Substitution players must differ.")
            if self.attacking_strategy is not None or self.defensive_strategy is not None:
                raise EngineStateError("Substitution command cannot contain tactical strategies.")

    def to_payload(self):
        return {
            "sequence": self.sequence,
            "command_ref": self.command_ref,
            "club_id": self.club_id,
            "command_type": self.command_type,
            "effective_segment": self.effective_segment,
            "attacking_strategy": self.attacking_strategy,
            "defensive_strategy": self.defensive_strategy,
            "player_out_id": self.player_out_id,
            "player_in_id": self.player_in_id,
        }
