import os

from rest_framework import serializers
from .models import Artista, Album, Song


class ArtistaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Artista
        fields = '__all__'  # Incluir todos los campos del modelo

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['foto'] = "artistas/" + os.path.basename(ret['foto'])

        return ret


class AlbumSerializer(serializers.ModelSerializer):
    artista_nombre = serializers.CharField(source='artist.nombre', read_only=True)

    class Meta:
        model = Album
        fields = ['name', 'year', 'cover_image', 'artista_nombre']

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['cover_image'] = "cover_image/" + os.path.basename(ret['cover_image'])

        return ret


class CancionSerializer(serializers.ModelSerializer):
    artista_nombre = serializers.CharField(source='artist.nombre', read_only=True)
    album_nombre = serializers.CharField(source='album.name', read_only=True)

    class Meta:
        model = Song
        fields = ['title', 'year', 'lyrics', 'pista', 'artista_nombre', 'album_nombre']
