from rest_framework import serializers
from ..models.glb import Glb
from ..models.customer import Customer

from rest_framework.exceptions import ValidationError
import os


class GlbSerializer(serializers.ModelSerializer):
    class Meta:
        model = Glb
        fields = ['id', 'url', 'glb', '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 'glb' in request.FILES:
            glb = request.FILES['glb']
            if glb.size > 50 * 1024 * 1024:  # 50MB limit
                raise serializers.ValidationError("Glb file too large ( > 50MB )")
            
            # Check file extension
            ext = os.path.splitext(Glb.name)[1].lower()
            valid_extensions = ['.glb']
            if ext not in valid_extensions:
                raise serializers.ValidationError(f"Unsupported file extension. Supported: {', '.join(valid_extensions)}")
        
        return data