import names
import json
import random
from typing import Optional, List, Set
from string import ascii_uppercase

from django.core.management.base import BaseCommand
from django.db.models import Q, Count

from mainapp.models import Member, Team, SectionValidator, Section


# TODO: refactor this file like create_teams_fixtures.py


class Command(BaseCommand):
    help = "Creates a fixture with randomly generated Member objects"

    def add_arguments(self, parser):
        parser.add_argument("members_per_team", type=int)

    def handle(self, *args, **options):
        members_per_team = options["members_per_team"]

        teams = Team.objects.all()

        if not teams:
            raise Exception("no teams")

        new_members = []
        for team in teams:

            i = members_per_team
            while i > 0:

                team_member_ids = team.members.values_list('id', flat=True)

                print(team_member_ids)

                section = random.choice([
                    sec for sec in 
                    Section.objects.exclude(members__id__in=team_member_ids)
                ])

                member = Member.objects.create(
                    first_name=names.get_first_name(),
                    last_name=names.get_last_name(),
                    section=section,
                    team=team
                )

                # if not section.is_joinable(member):
                #     member.delete()
                #     i += 1
                #     continue

                new_members.append(member)
                print(member)

                i -= 1

        fixtures = []
        for member in new_members:
            member.delete()

            fixture = {
                "model": "mainapp.member",
                "pk": member.pk,
                "fields": {
                    "first_name": member.first_name,
                    "last_name": member.last_name,
                    "team": member.team_id,
                    "section": member.section_id,
                    "peg_number": member.peg_number
                }
            }
            fixtures.append(fixture)

        with open("src/mainapp/fixtures/members_fixture.json", "w") as f:
            f.write(json.dumps(fixtures, indent=2))
            self.stdout.write(self.style.SUCCESS("Created members_fixture.json."))

    # def handle(self, *args, **options):
    #     num_members = options["num_members"]
    #     teams = Team.objects.all()
    #     if not teams:
    #         print("No teams found. Please create some teams first.")
    #         return

    #     members = []
    #     for team in teams:
    #         for _ in range(num_members):
    #             first_name = names.get_first_name()
    #             last_name = names.get_last_name()
    #             member = Member.objects.create(first_name=first_name, last_name=last_name, team=team)
    #             members.append(member)

    #     self.stdout.write(self.style.SUCCESS(f"Created {num_members} members."))

    #     # create a members fixture file
    #     members_fixture = []
    #     for member in members:
    #         member_fixture = {
    #             "model": "mainapp.member",
    #             "pk": member.pk,
    #             "fields": {
    #                 "first_name": member.first_name,
    #                 "last_name": member.last_name,
    #                 "team": member.team_id,
    #                 "peg_number": member.peg_number,
    #             }
    #         }
    #         members_fixture.append(member_fixture)

    #     with open("src/mainapp/fixtures/members_fixture.json", "w") as f:
    #         f.write(json.dumps(members_fixture, indent=2))
    #         self.stdout.write(self.style.SUCCESS("Created members_fixture.json."))
