import os

from core import serializers
from core.validators import ValidateFileExtension
from storage.models import DataStorage


class AntivirusStorageSerializer(serializers.ModelLocalizedSerializer):
    type = serializers.IntegerField(default=DataStorage.Type.CLAMAV, initial=DataStorage.Type.CLAMAV)
    format = serializers.IntegerField(default=DataStorage.Format.ZIP, initial=DataStorage.Format.ZIP)
    file = serializers.FileField(validators=[ValidateFileExtension(allowed_extensions=[".zip"])])
    description = serializers.CharField(default='ClamAV DB')

    class Meta:
        model = DataStorage
        fields = '__all__'


class StorageListSerializer(serializers.ModelLocalizedSerializer):
    format = serializers.CharField(source='get_format_display')
    filename = serializers.SerializerMethodField()

    class Meta:
        model = DataStorage
        fields = ['id', 'format', 'size', 'description', 'created', 'filename']

    def get_filename(self, instance):
        return os.path.basename(instance.file.name)


class StorageSerializer(StorageListSerializer):
    format = serializers.CharField(source='get_format_display')

    class Meta:
        model = DataStorage
        fields = '__all__'


class DataStorageTypeSerializer(serializers.Serializer):
    """ To get all storage types for a user """
    id = serializers.IntegerField()
    name = serializers.CharField()
