from django.db import models
from django.db.models import Prefetch
from django.apps import apps

class DeckNameTranslation(models.Model):
  id = models.AutoField(primary_key=True)
  name = models.CharField(max_length=64)
  deck = models.ForeignKey("Deck", on_delete=models.PROTECT, related_name='name_translations')
  language = models.CharField(max_length=64)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __str__(self):
    return self.name

class CardNameTranslation(models.Model):
  id = models.AutoField(primary_key=True)
  name = models.CharField(max_length=64)
  card = models.ForeignKey("Card", on_delete=models.PROTECT, related_name='name_translations')
  language = models.CharField(max_length=64)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __str__(self):
    return self.name
class Deck(models.Model):
  id = models.AutoField(primary_key=True)
  name = models.CharField(max_length=64)
  hex_color = models.CharField(max_length=9)
  cardset = models.CharField(max_length=8)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __str__(self):
    return self.name

class Card(models.Model):
  id = models.AutoField(primary_key=True)
  name = models.CharField(max_length=64)
  decks = models.ManyToManyField("Deck")
  cardset = models.CharField(max_length=32)
  cardnum = models.IntegerField()
  style = models.CharField(max_length=128)
  rarity_icon = models.CharField(max_length=12)
  rarity_level = models.IntegerField()
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  class Meta:
    unique_together = ('cardset', 'cardnum')

  def __str__(self):
    return f"{self.name} ({self.cardset} #{self.cardnum})"