from django.db import models
from django.contrib.auth.models import User
import uuid
import random
from datetime import datetime

class Session(models.Model):
    def __str__(self):
        return f"Session {self.session_id} - Host: {self.host.username}"

    @property
    def is_open(self):
        return (datetime.now() < self.opens) and self.voting_open

    opens = models.DateTimeField(null=True, default=None)
    session_id = models.UUIDField(default=uuid.uuid4, editable=False)
    host = models.ForeignKey(User, on_delete=models.CASCADE)
    players_wanted = models.ManyToManyField(User, related_name='players_wanted')
    players_connected= models.ManyToManyField(User, related_name='players_joined', blank=True)
    seed = models.IntegerField(default=random.randint(1, 1000000))
    status = models.CharField(
        max_length=30,
        choices=[
            ('lobby', 'Lobby'),
            ('voting', 'Voting'),
            ('waiting_for_next_track', 'Waiting for Next Track'),
            ('paused', 'Paused'),
            ('voting_done', 'Voting Done'),
        ],
        default='lobby'
    )
    current_track = models.ForeignKey('Track', on_delete=models.SET_NULL, null=True, blank=True)
    voting_open = models.BooleanField(default=False)
