from .models import User,UserCustomization,ProductMaterial,CigarProduct,LoginLog,Product_Image
from rest_framework import serializers



class UserRegistrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
    



class UserLoginSerializer(serializers.Serializer):
    email_mobile = serializers.CharField(max_length=255, required=True)
    password = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})

    def validate(self, data):
        email_mobile = data.get('email_mobile')
        password = data.get('password')
        if email_mobile and password:
            user = None
            if '@' in email_mobile:
                user = User.objects.filter(EMAIL_ID=email_mobile,PASSWORD=password).exclude(IS_DELETED=True)
            elif email_mobile.isdigit():
                user = User.objects.filter(MOBILE_NO=email_mobile,PASSWORD=password).exclude(IS_DELETED=True)
            if user :
                return user
        raise serializers.ValidationError('Invalid credentials')
    

class UserCustomizationSerializers(serializers.Serializer):
    class Meta:
        model = UserCustomization
        fields = '_all_'



class ProductMaterialSerializers(serializers.Serializer):
    class Meta:
        model = ProductMaterial
        fields = '_all_'


class CigarProductSerializers(serializers.Serializer):
    class Meta:
        model = CigarProduct
        fields = '_all_'


class LoginLogSerializers(serializers.Serializer):
    def create(self, validated_data):
        # Assuming that the LoginLogs model is associated with the serializer
        # Adjust the fields accordingly based on your model definition
        return LoginLog.objects.create(**validated_data)
    class Meta:
        model = LoginLog
        fields = '_all_'


class PersonalInfoSerializers(serializers.Serializer):
    class Meta:
        model= User
        fields= "__all__" 



class ProductImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product_Image
        fields = '__all__'