from rest_framework import serializers

from fwd import settings
from fwd_api.constant.common import FileCategory, FolderFileType
from fwd_api.models import SubscriptionRequestFile, SubscriptionRequest
from fwd_api.utils import FileUtils


class ReportFileSerializer(serializers.Serializer):
    file_name = serializers.CharField(read_only=True)
    file_url = serializers.SerializerMethodField()
    file_category = serializers.CharField(max_length=200, default=FileCategory.Origin.value)

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return SubscriptionRequestFile.objects.create(**validated_data)

    def update(self, instance, validated_data):
        return instance

    def get_file_url(self, obj: SubscriptionRequestFile):
        rq: SubscriptionRequest = obj.request
        return FileUtils.build_url(FolderFileType.REQUESTS.value, rq.request_id, rq.subscription.user.id, obj.file_name)
