from django.db import models
import uuid

class TournamentPayment(models.IntegerChoices):
    FREE = 0, 'Gratuit'
    UNIT = 1, 'Unité'
    SUBSCRIPTION_UNIT = 2, 'Unité abonnement'
    UNLIMITED = 3, 'Illimité'

class FederalCategory(models.IntegerChoices):
    MEN = 0, 'Homme'
    WOMEN = 1, 'Femme'
    MIXED = 2, 'Mixte'
    UNLISTED = 3, ''

    @staticmethod
    def female_in_male_assimilation_addition(rank: int) -> int:
        if rank is None:
            return 0
        if 1 <= rank <= 10:
            return 400
        elif 11 <= rank <= 30:
            return 1000
        elif 31 <= rank <= 60:
            return 2000
        elif 61 <= rank <= 100:
            return 3500
        elif 101 <= rank <= 200:
            return 10000
        elif 201 <= rank <= 500:
            return 15000
        elif 501 <= rank <= 1000:
            return 25000
        elif 1001 <= rank <= 2000:
            return 35000
        elif 2001 <= rank <= 3000:
            return 45000
        else:
            return 50000


class FederalLevelCategory(models.IntegerChoices):
    UNLISTED = 0, 'Animation'
    P25 = 25, 'P25'
    P100 = 100, 'P100'
    P250 = 250, 'P250'
    P500 = 500, 'P500'
    P1000 = 1000, 'P1000'
    P1500 = 1500, 'P1500'
    P2000 = 2000, 'P2000'

    @staticmethod
    def min_player_rank(level=None, category=None, age_category=None) -> int:
        if level == FederalLevelCategory.P25:
            if age_category in [FederalAgeCategory.SENIOR, FederalAgeCategory.A45, FederalAgeCategory.A55]:
                return 20000 if category == FederalCategory.MEN else 1000
            return 0

        elif level == FederalLevelCategory.P100:
            if age_category in [FederalAgeCategory.SENIOR, FederalAgeCategory.A45, FederalAgeCategory.A55]:
                return 2000 if category == FederalCategory.MEN else 300
            return 0

        elif level == FederalLevelCategory.P250:
            if age_category in [FederalAgeCategory.SENIOR, FederalAgeCategory.A45, FederalAgeCategory.A55]:
                if category == FederalCategory.MIXED:
                    return 0
                return 500 if category == FederalCategory.MEN else 100
            return 0

        return 0


class FederalAgeCategory(models.IntegerChoices):
    UNLISTED = 0, ''
    A11_12 = 120, 'U12'
    A13_14 = 140, 'U14'
    A15_16 = 160, 'U16'
    A17_18 = 180, 'U18'
    SENIOR = 200, 'Senior'
    A45 = 450, '+45 ans'
    A55 = 550, '+55 ans'

class FederalMatchCategory(models.IntegerChoices):
    TWO_SETS = 0, 'Two sets'
    TWO_SETS_SUPER_TIE = 1, 'Two sets super tie'
    TWO_SETS_FOUR_GAME = 2, 'Two sets of four games'
    NINE_GAMES = 3, 'Nine games'
    SUPER_TIE = 4, 'Super Tie-Break'
    MEGA_TIE = 5, 'Mega Tie-Break'
    TWO_SETS_DECISIVE_POINT = 6, 'Two Sets with decisive point'
    TWO_SETS_DECISIVE_POINT_SUPER_TIE = 7, 'Two Sets with decisive point and super tie-break'
    TWO_SETS_FOUR_GAME_DECISIVE_POINT = 8, 'Two sets of four games with decisive point'
    NINE_GAMES_DECISIVE_POINT = 9, 'Nine games with decisive point'
    TWO_SETS_OF_SUPER_TIE = 10, 'Two sets of Super Tie-Break decisive point'
    SINGLE_SET = 11, 'Single set'
    SINGLE_SET_DECISIVE_POINT = 12, 'Single set with decisive point'
    SINGLE_SET_OF_FOUR_GAMES = 13, 'Single set of four games'
    SINGLE_SET_OF_FOUR_GAMES_DECISIVE_POINT = 14, 'Single set of four games with decisive point'

    @property
    def format_label_short(self):
        format_mapping = {
            self.TWO_SETS: "A1",
            self.TWO_SETS_SUPER_TIE: "B1",
            self.TWO_SETS_FOUR_GAME: "C1",
            self.NINE_GAMES: "D1",
            self.SUPER_TIE: "E",
            self.TWO_SETS_OF_SUPER_TIE: "G",
            self.MEGA_TIE: "F",
            self.SINGLE_SET: "H1",
            self.SINGLE_SET_DECISIVE_POINT: "H2",
            self.TWO_SETS_DECISIVE_POINT: "A2",
            self.TWO_SETS_DECISIVE_POINT_SUPER_TIE: "B2",
            self.TWO_SETS_FOUR_GAME_DECISIVE_POINT: "C2",
            self.NINE_GAMES_DECISIVE_POINT: "D2",
            self.SINGLE_SET_OF_FOUR_GAMES: "I1"
        }
        return format_mapping.get(self, "")

    def last_set_is_tie_break(value):
        if value == FederalMatchCategory.TWO_SETS_FOUR_GAME or value == FederalMatchCategory.TWO_SETS_FOUR_GAME_DECISIVE_POINT or value == FederalMatchCategory.TWO_SETS_SUPER_TIE or value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.TWO_SETS_DECISIVE_POINT_SUPER_TIE:
            return True
        else:
            return False

    def max_number_of_sets(value):
        if value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.NINE_GAMES or value == FederalMatchCategory.NINE_GAMES_DECISIVE_POINT or value == FederalMatchCategory.SINGLE_SET or value == FederalMatchCategory.SINGLE_SET_DECISIVE_POINT or value == FederalMatchCategory.SINGLE_SET_OF_FOUR_GAMES or value == FederalMatchCategory.SINGLE_SET_OF_FOUR_GAMES_DECISIVE_POINT:
            return 1
        else:
            return 3

class ModelOperation(models.TextChoices):
    POST = 'POST', 'POST'
    PUT = 'PUT', 'PUT'
    DELETE = 'DELETE', 'DELETE'
    GRANT_ACCESS = 'GRANT_ACCESS', 'GRANT_ACCESS'
    REVOKE_ACCESS = 'REVOKE_ACCESS', 'REVOKE_ACCESS'

class OnlineRegistrationStatus(models.IntegerChoices):
    OPEN = 1, 'Open'
    NOT_ENABLED = 2, 'Not Enabled'
    NOT_STARTED = 3, 'Not Started'
    ENDED = 4, 'Ended'
    WAITING_LIST_POSSIBLE = 5, 'Waiting List Possible'
    WAITING_LIST_FULL = 6, 'Waiting List Full'
    IN_PROGRESS = 7, 'In Progress'
    ENDED_WITH_RESULTS = 8, 'Ended with Results'

    def status_localized(self) -> str:
        status_map = {
            OnlineRegistrationStatus.OPEN: "Inscription ouverte",
            OnlineRegistrationStatus.NOT_ENABLED: "Inscription désactivée",
            OnlineRegistrationStatus.NOT_STARTED: "Ouverture des inscriptions à venir",
            OnlineRegistrationStatus.ENDED: "Inscription terminée",
            OnlineRegistrationStatus.WAITING_LIST_POSSIBLE: "Liste d'attente ouverte",
            OnlineRegistrationStatus.WAITING_LIST_FULL: "Liste d'attente complète",
            OnlineRegistrationStatus.IN_PROGRESS: "Tournoi en cours",
            OnlineRegistrationStatus.ENDED_WITH_RESULTS: "Tournoi terminé"
        }
        return status_map.get(self, "")

class UserOrigin(models.IntegerChoices):
    ADMIN = 0, 'Admin'
    SITE = 1, 'Site'
    APP = 2, 'App'
