from typing import Optional

from src.models import ScibetMatch
from src.settings import match_cache, import_league_colors, Colors


@match_cache
def prediction(match: ScibetMatch) -> Optional[str]:
    if 1.5 < match.to2_5_odds < 1.98:
        if match.away_odds > 2:
            if (
                match.draw_odds >= 3.55
                and 0 < match.home_attack < 97
                and match.home_attack - match.home_defense > 27
                and match.home_attack - match.away_attack > 22
                and match.away_attack - match.away_defense > 20
            ):
                return 'H'
        if match.home_odds > 4:
            if (
                match.draw_odds >= 3.7
                and match.home_odds < 9.5
                and 0 < match.away_attack < 97
                and match.home_attack > 7
                and 0 < match.home_defense < 34
                and match.away_defense < 70
                and match.home_attack - match.home_defense > -20
                and match.away_attack - match.away_defense > 14
            ):
                return 'A'
        if match.draw_odds >= 2:
            if (
                match.home_odds > 2
                and match.away_odds > 2
                and 0 < match.home_defense < 45
                and match.away_defense > 20
                and 85 > match.away_attack > 60
                and match.home_attack - match.home_defense > 20
                and match.away_attack - match.away_defense > 25
            ):
                return 'M'
    return None


@match_cache
def league_color(match: ScibetMatch, variation: Optional[str] = None) -> Optional[Colors]:
    return import_league_colors('v19', variation)(prediction(match), match.league)
