from rest_framework import serializers
from ..models.video import Video
from ..models.customer import Customer

from rest_framework.exceptions import ValidationError
import os


class VideoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = ['id', 'url', 'video', 'name', 'created_at']
        read_only_fields = ['id', 'created_at', 'user']
    
    def validate(self, data):
        # Safely get the request from context
        request = self.context.get('request')
        if not request:
            raise serializers.ValidationError("Request context not provided")
        
        # Check file size if image is provided
        if 'video' in request.FILES:
            video = request.FILES['video']
            if video.size > 500 * 1024 * 1024:  # 500MB limit
                raise serializers.ValidationError("Video file too large ( > 500MB )")
            
            # Check file extension
            ext = os.path.splitext(Video.name)[1].lower()
            valid_extensions = ['.mp4']
            if ext not in valid_extensions:
                raise serializers.ValidationError(f"Unsupported file extension. Supported: {', '.join(valid_extensions)}")
        
        return data