from django.db import models
import datetime
from django.core.validators import MaxValueValidator, MinValueValidator


def current_year():
    return datetime.date.today().year


def max_value_current_year(value):
    return MaxValueValidator(current_year())(value)


class Artista(models.Model):
    nombre = models.CharField(max_length=200)
    biografia = models.TextField(blank=True, null=True)
    foto = models.ImageField(upload_to='artistas/', blank=True, null=True)  # Nuevo campo

    def __str__(self):
        return self.nombre


class Album(models.Model):
    name = models.CharField(max_length=200)
    artist = models.ForeignKey(Artista, on_delete=models.CASCADE)
    year = models.PositiveBigIntegerField(default=current_year(), validators=[MinValueValidator(1945), max_value_current_year])
    cover_image = models.ImageField(upload_to='cover_image/', blank=True, null=True)  # Nuevo campo

    def __str__(self):
        return self.name


class Song(models.Model):
    title = models.CharField(max_length=200)
    artist = models.ForeignKey(Artista, on_delete=models.CASCADE)
    album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='song')
    year = models.DecimalField(max_digits=4, decimal_places=0, blank=False, null=False)
    lyrics = models.TextField()
    pista = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)

    def __str__(self):
        return self.title
