from django.utils import timezone
from .models import TeamRegistration, PlayerRegistration
from .models.player_enums import PlayerSexType, PlayerDataSource
from .models.enums import FederalCategory
from tournaments.utils.licence_validator import LicenseValidator

class TournamentRegistrationRepository:
    @staticmethod
    def create_team_registration(tournament, registration_date):
        team_registration = TeamRegistration.objects.create(
            tournament=tournament,
            registration_date=registration_date
        )
        return team_registration

    @staticmethod
    def create_player_registrations(request, team_registration, players_data, team_form_data):
        stripped_license = None
        if request.user.is_authenticated and request.user.licence_id:
            stripped_license = LicenseValidator(request.user.licence_id).stripped_license

        for player_data in players_data:
            is_captain = False
            player_licence_id = player_data['licence_id']
            if player_licence_id and stripped_license:
                if stripped_license.lower() in player_licence_id.lower():
                    is_captain = True

            sex, rank, computed_rank = TournamentRegistrationRepository._compute_rank_and_sex(
                team_registration.tournament,
                player_data
            )

            print("create_player_registrations", player_data.get('last_name'), sex, rank, computed_rank)
            data_source = None
            if player_data.get('found_in_french_federation', False) == True:
                data_source = PlayerDataSource.FRENCH_FEDERATION

            player_registration = PlayerRegistration.objects.create(
                team_registration=team_registration,
                captain=is_captain,
                source=data_source,
                registered_online=True,
                first_name=player_data.get('first_name'),
                last_name=player_data.get('last_name'),
                points=player_data.get('points'),
                assimilation=player_data.get('assimilation'),
                tournament_played=player_data.get('tournament_count'),
                ligue_name=player_data.get('ligue_name'),
                club_name=player_data.get('club_name'),
                birthdate=player_data.get('birth_year'),
                sex=sex,
                rank=rank,
                computed_rank=computed_rank,
                licence_id=player_data['licence_id'],
                email=player_data.get('email'),
                phone_number=player_data.get('mobile_number'),
            )

            player_registration.save()

        team_registration.set_weight()
        team_registration.save()

    @staticmethod
    def _compute_rank_and_sex(tournament, player_data):
        is_woman = player_data.get('is_woman', False)
        rank = player_data.get('rank', None)
        if rank is None:
            computed_rank = 100000
        else:
            computed_rank = rank

        sex = PlayerSexType.MALE
        if is_woman:
            sex = PlayerSexType.FEMALE
            if tournament.federal_category == FederalCategory.MEN:
                computed_rank = str(int(computed_rank) +
                    FederalCategory.female_in_male_assimilation_addition(int(rank)))

        print("_compute_rank_and_sex", sex, rank, computed_rank)
        return sex, rank, computed_rank
