from django.db import models

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, season_year: None) -> int:
        if rank is None:
            return 0

        if season_year == 2026:
            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
            elif 3001 <= rank <= 5000:
                return 55000
            elif 5001 <= rank <= 10000:
                return 70000
            else:
                return 90000

        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'
    CHPT = 1, 'Championnat'

    def localized_word(self):
        if self == FederalLevelCategory.UNLISTED:
            return "animation"
        elif self == FederalLevelCategory.CHPT:
            return "championnat"
        else:
            return "tournoi"

    def is_feminine_word(self):
        if self == FederalLevelCategory.UNLISTED:
            return True
        else:
            return False

    def localized_prefix_at(self):
        if self == FederalLevelCategory.UNLISTED:
            return "à l'"
        else:
            return "au "

    def localized_prefix_of(self):
        if self == FederalLevelCategory.UNLISTED:
            return "de l'"
        else:
            return "du "

    def localized_prefix_this(self):
        if self == FederalLevelCategory.UNLISTED:
            return "cette"
        else:
            return "ce "

    def localized_prefix_that(self):
        if self == FederalLevelCategory.UNLISTED:
            return "l'"
        else:
            return "le "

    @staticmethod
    def min_player_rank(level=None, category=None, age_category=None, season_year=None) -> int:
        if season_year == 2026 and category == FederalCategory.MIXED:
            return 0

        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, ''
    A09_10 = 100, 'U10'
    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'
    CANCELED = 9, 'Canceled'

    def display_register_option(self) -> bool:
        status_map = {
            OnlineRegistrationStatus.OPEN: True,
            OnlineRegistrationStatus.NOT_ENABLED: False,
            OnlineRegistrationStatus.NOT_STARTED: False,
            OnlineRegistrationStatus.ENDED: False,
            OnlineRegistrationStatus.WAITING_LIST_POSSIBLE: True,
            OnlineRegistrationStatus.WAITING_LIST_FULL: False,
            OnlineRegistrationStatus.IN_PROGRESS: False,
            OnlineRegistrationStatus.ENDED_WITH_RESULTS: False,
            OnlineRegistrationStatus.CANCELED: False
        }
        return status_map.get(self, False)

    def register_button_text(self) -> str:
        if self == OnlineRegistrationStatus.OPEN:
            return "S'inscrire"
        elif self == OnlineRegistrationStatus.NOT_ENABLED:
            return "Inscription désactivée"
        elif self == OnlineRegistrationStatus.NOT_STARTED:
            return "Ouverture des inscriptions à venir"
        elif self == OnlineRegistrationStatus.ENDED:
            return "Inscription terminée"
        elif self == OnlineRegistrationStatus.WAITING_LIST_POSSIBLE:
            return "S'inscrire en liste d'attente"
        elif self == OnlineRegistrationStatus.WAITING_LIST_FULL:
            return "Liste d'attente complète"
        elif self == OnlineRegistrationStatus.IN_PROGRESS:
            return "Tournoi en cours"
        elif self == OnlineRegistrationStatus.ENDED_WITH_RESULTS:
            return "Tournoi terminé"
        elif self == OnlineRegistrationStatus.CANCELED:
            return "Tournoi annulé"
        return ""

    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é",
            OnlineRegistrationStatus.CANCELED: "Tournoi annulé"
        }
        return status_map.get(self, "")

    def short_label(self) -> str:
        """Returns a short, concise label for the status box"""
        label_map = {
            OnlineRegistrationStatus.OPEN: "ouvert",
            OnlineRegistrationStatus.NOT_ENABLED: "désactivé",
            OnlineRegistrationStatus.NOT_STARTED: "à venir",
            OnlineRegistrationStatus.ENDED: "clôturé",
            OnlineRegistrationStatus.WAITING_LIST_POSSIBLE: "ouvert",
            OnlineRegistrationStatus.WAITING_LIST_FULL: "complet",
            OnlineRegistrationStatus.IN_PROGRESS: "en cours",
            OnlineRegistrationStatus.ENDED_WITH_RESULTS: "résultats",
            OnlineRegistrationStatus.CANCELED: "annulé"
        }
        return label_map.get(self, "")

    def box_class(self) -> str:
        """Returns the CSS class for the status box"""
        class_map = {
            OnlineRegistrationStatus.OPEN: "light-green",
            OnlineRegistrationStatus.NOT_ENABLED: "gray",
            OnlineRegistrationStatus.NOT_STARTED: "light-green",
            OnlineRegistrationStatus.ENDED: "gray",
            OnlineRegistrationStatus.WAITING_LIST_POSSIBLE: "light-orange",
            OnlineRegistrationStatus.WAITING_LIST_FULL: "light-red",
            OnlineRegistrationStatus.IN_PROGRESS: "blue",
            OnlineRegistrationStatus.ENDED_WITH_RESULTS: "dark-gray",
            OnlineRegistrationStatus.CANCELED: "light-red",
        }
        return class_map.get(self, "gray")

    def display_box(self) -> bool:
        """
        Determines whether this status should display a status box
        Returns True if the status should be displayed, False otherwise
        """
        # List the statuses that should display a box
        display_statuses = [
            OnlineRegistrationStatus.OPEN,
            OnlineRegistrationStatus.NOT_STARTED,
            OnlineRegistrationStatus.WAITING_LIST_POSSIBLE,
            OnlineRegistrationStatus.WAITING_LIST_FULL,
            OnlineRegistrationStatus.CANCELED,
            # You can add or remove statuses as needed
        ]

        return self in display_statuses

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

class UserRole(models.IntegerChoices):
    JAP = 0, 'Juge-Arbitre'
    CLUB_OWNER = 1, 'Club Owner'
    PLAYER = 2, 'Player'

class RegistrationStatus(models.IntegerChoices):
    WAITING = 0, 'Waiting'
    PENDING = 1, 'Pending'
    CONFIRMED = 2, 'Confirmed'
    CANCELED = 3, 'Canceled'

class RegistrationPaymentMode(models.IntegerChoices):
    DISABLED = 0, 'Disabled'
    CORPORATE = 1, 'Corporate'
    NO_FEE = 2, 'No Service Fee'
    STRIPE = 3, 'Stripe'

class AnimationType(models.IntegerChoices):
    TOURNAMENT = 0, 'Tournoi'
    MELEE = 1, 'Mêlée'
    LOSER_BRACKET = 2, 'Classement'
    CONSOLATION_BRACKET = 3, 'Consolation'
    CUSTOM = 4, 'Custom'
