from django.db import models

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.db.models import IntegerField

from games.consumers import game_manager

def upload_to(instance, filename: str):
    return f"./profiles/static/avatars/{instance.pk}.{filename.split('.')[1]}"

# Create your models here.
class ProfileModel(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    avatar_url = models.ImageField(upload_to=upload_to, default="./profiles/static/avatars/default.avif") #blank=True, null=True)
    
    def get_game(self) -> int:
        for game in game_manager._game_list:
            for player in game.get_players_connected():
                if (player.user_id == self.user.pk):
                    return game.game_id
        return None

@receiver(post_save, sender=User)
def on_user_created(sender, instance, created, **kwargs):
    if created:
        profile: ProfileModel = ProfileModel.objects.create(pk = instance.pk, user = instance)
        profile.save()
		
class BlockModel(models.Model):
	blocker = IntegerField(primary_key=False)
	blocked = IntegerField(primary_key=False)

	def __str__(self):
		return "blocker_id: " + str(self.blocker) + ", blocked_id: " + str(self.blocked)
