import uuid
from django.db import models
from nominations.models import Evenement, Categorie, Candidat

class Vote(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    voter_hash = models.CharField(max_length=64, db_index=True)
    evenement = models.ForeignKey(Evenement, on_delete=models.CASCADE)
    categorie = models.ForeignKey(Categorie, on_delete=models.CASCADE)
    candidat = models.ForeignKey(Candidat, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('voter_hash', 'categorie') # Empêche le double vote

    def __str__(self):
        return f"{self.voter_hash[:8]} voté dans {self.categorie.nom}"