from django.db import models
from django.db.models.sql.query import Q
from . import Tournament
import uuid
from django.utils import timezone

class UnregisteredTeam(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
    tournament = models.ForeignKey(Tournament, on_delete=models.SET_NULL, related_name='unregistered_teams', null=True, blank=True)
    unregistration_date = models.DateTimeField(null=True, blank=True)

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

    def player_names_as_list(self):
        return [pr.name() for pr in self.unregistered_players.all()]

    def team_names(self):
        return self.player_names_as_list()

    def shortened_team_names(self):
        players = list(self.unregistered_players.all())
        if len(players) == 1:
            return [players[0].shortened_name(), '']
        else:
            return [pr.shortened_name() for pr in players]

    @property
    def players(self):
        # Fetch related PlayerRegistration objects
        return self.unregistered_players.all().order_by('last_name')

    def player_names(self):
        names = self.player_names_as_list()
        str = " - ".join(names)
        if len(str) > 0:
            return str
        else:
            return "no players"
