from django.db import models
from . import SideStoreModel, TeamRegistration, PlayerSexType, PlayerDataSource, PlayerPaymentType, FederalCategory
import uuid
from django.utils import timezone

class PlayerRegistration(SideStoreModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
    team_registration = models.ForeignKey(TeamRegistration, on_delete=models.SET_NULL, related_name='player_registrations', null=True)
    first_name = models.CharField(max_length=50, blank=True)
    last_name = models.CharField(max_length=50, blank=True)
    licence_id = models.CharField(max_length=50, null=True, blank=True)
    rank = models.IntegerField(null=True, blank=True)
    #has_paid = models.BooleanField(default=False)
    #unranked = models.BooleanField(default=False)

    payment_type = models.IntegerField(choices=PlayerPaymentType.choices, null=True, blank=True)
    #registration_date = models.DateTimeField(null=True, blank=True)
    sex = models.IntegerField(choices=PlayerSexType.choices, null=True, blank=True)

    #donnee publique
    tournament_played = models.IntegerField(null=True, blank=True)
    points = models.FloatField(null=True, blank=True)
    club_name = models.CharField(max_length=200, null=True, blank=True)
    ligue_name = models.CharField(max_length=200, null=True, blank=True)
    assimilation = models.CharField(max_length=50, null=True, blank=True)

    #beachpadel
    phone_number = models.CharField(max_length=50, null=True, blank=True)
    email = models.CharField(max_length=50, null=True, blank=True)

    birthdate = models.CharField(max_length=50, null=True, blank=True)

    computed_rank = models.IntegerField(default=0)

    source = models.IntegerField(choices=PlayerDataSource.choices, null=True, blank=True)
    has_arrived = models.BooleanField(default=False)
    captain = models.BooleanField(default=False)
    coach = models.BooleanField(default=False)
    registered_online = models.BooleanField(default=False)

    def delete_dependencies(self):
        pass

    def __str__(self):
        return self.name()

    def save(self, *args, **kwargs):
        self.store_id = str(self.get_tournament_id())
        super().save(*args, **kwargs)

    def get_tournament_id(self):
        if self.team_registration and self.team_registration.tournament:
            return self.team_registration.tournament.id
        else:
            return None

    def name(self):
        return f"{self.first_name} {self.last_name}"

    def shortened_name(self):
        name = self.name()
        if len(name) > 20 and self.first_name:
            name = f"{self.first_name[0]}. {self.last_name}"
        if len(name) > 20:
            name_parts = self.last_name.split(" ")
            name = f"{self.first_name[0]}. {name_parts[0]}"
        return name

    def clean_club_name(self):
        if self.club_name:
            return self.club_name.split(' (')[0]
        return "Non renseigné"

    def calculate_age(self):
        if self.birthdate:
            try:
                birth_year = int(self.birthdate[-4:])  # Assumes birthdate ends with YYYY
                current_year = timezone.now().year
                return current_year - birth_year
            except (ValueError, TypeError, IndexError):
                return None
        return None

    def format_ordinal(self):
        if self.rank is None:
            if self.sex == PlayerSexType.FEMALE:
                return "Non Classée"
            return "Non Classé"

        if self.rank == 1:
            if self.sex == PlayerSexType.FEMALE:
                return "1ère"
            return "1er"
        return f"{self.rank}ème"
