from rest_framework import serializers
from taggit.serializers import TaggitSerializer, TagListSerializerField

from ..models import Track, UserSettings

__all__ = ("TrackSerializer", "UserSettingsSerializer")


class TrackSerializer(TaggitSerializer, serializers.ModelSerializer):
    tags = TagListSerializerField()

    class Meta:
        model = Track
        fields = (
            "pk",
            "artist",
            "title",
            "album",
            "year",
            "position",
            "info",
            "tags",
            "episode",
            "sound",
            "timestamp",
        )


class UserSettingsSerializer(serializers.ModelSerializer):
    # TODO: validate fields values (playlist_editor_columns at least)
    class Meta:
        model = UserSettings
        fields = ("playlist_editor_columns", "playlist_editor_sep")

    def create(self, validated_data):
        user = self.context.get("user")
        if user:
            validated_data["user_id"] = user.id
        return super().create(validated_data)
