from rest_framework import serializers
from mama_care_notification.models import *
from mama_care.serializers import *

class UserDeviceSerializer(serializers.ModelSerializer):
    isAuthorized = serializers.SerializerMethodField()
    class Meta:
        model = UserDevice
        fields = [
            "id",
            "token",
            "createdDate",
            "updatedDate",
            "parentProfileId",
            "deviceOSTypeId",
            "isAuthorized",
        ]

    def get_isAuthorized(self,instance):
        if isValidData(self,instance.parentProfileId.childId.id):
            return True
    def validate(self, data):
        errorMessage = ""
        isCheck = False
        if "parentProfileId" in data:
            isCheck = validateParentId(self,data["parentProfileId"].childId.parentId.id)
        else:
            errorMessage = "parentId", "Parent Id does not exit"
        
        if isCheck == True:
            return data
        
        elif len(errorMessage) > 0:
            raise serializers.ValidationError({"error":[errorMessage]})
        
class NotificationSerializer(serializers.ModelSerializer):
    isAuthorized = serializers.SerializerMethodField()
    class Meta:
        model = Notification
        fields = [
            "id",
            "title",
            "body",
            "createdDate",
            "updatedDate",
            "userDeviceId",
            "isAuthorized",
        ]

    def get_isAuthorized(self,instance):
        if isValidData(self,instance.userDeviceId.parentProfileId.childId.id):
            return True

    def validate(self, data):
        errorMessage = ""
        isCheck = False
        if "kickTrackerGroupId" in data:
            isCheck = validateParentId(self,data["userDeviceId"].parentProfileId.childId.parentId.id)
        else:
            errorMessage = "parentId", "Parent Id does not exit"
        
        if isCheck == True:
            return data
        
        elif len(errorMessage) > 0:
            raise serializers.ValidationError({"error":[errorMessage]})
