from django.contrib.auth.validators import UnicodeUsernameValidator
from rest_framework import serializers
from django.contrib.auth.models import User

from recipes.models import Recipe, Ingredient, Step


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("username", "last_name", "first_name", "email")
        extra_kwargs = {
            'username': {
                'validators': [UnicodeUsernameValidator()],
            }
        }


class IngredientSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ingredient
        fields = ["text"]


class StepSerializer(serializers.ModelSerializer):
    class Meta:
        model = Step
        fields = ["step_text"]


class RecipeSerializer(serializers.ModelSerializer):
    ingredients = IngredientSerializer(many=True, required=False)
    steps = StepSerializer(many=True, required=False)
    user = UserSerializer(required=True)

    def create(self, validated_data):
        steps_data = validated_data.pop('steps')
        ingredients_data = validated_data.pop('ingredients')
        user_data = validated_data.pop('user')
        username = user_data.pop('username')
        user = User.objects.get_by_natural_key(username)
        recipe = Recipe.objects.create(user=user, **validated_data)
        for steps in steps_data:
            Step.objects.create(recipe=recipe, **steps)

        for ingredients in ingredients_data:
            Ingredient.objects.create(recipe=recipe, **ingredients)
        return recipe

    class Meta:
        model = Recipe
        fields = ("name", "user", "steps", "ingredients")

    def update(self, instance, validated_data):
        steps_data = validated_data.pop('steps')
        ingredients_data = validated_data.pop('ingredients')
        Step.objects.filter(recipe=instance).delete()
        Ingredient.objects.filter(recipe=instance).delete()
        for steps in steps_data:
            Step.objects.create(recipe=instance, **steps)

        for ingredients in ingredients_data:
            Ingredient.objects.create(recipe=instance, **ingredients)
        return instance

[((1216, 1257), 'django.contrib.auth.models.User.objects.get_by_natural_key', 'User.objects.get_by_natural_key', (['username'], {}), '(username)\n', (1247, 1257), False, 'from django.contrib.auth.models import User\n'), ((1275, 1325), 'recipes.models.Recipe.objects.create', 'Recipe.objects.create', ([], {'user': 'user'}), '(user=user, **validated_data)\n', (1296, 1325), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((1371, 1414), 'recipes.models.Step.objects.create', 'Step.objects.create', ([], {'recipe': 'recipe'}), '(recipe=recipe, **steps)\n', (1390, 1414), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((1473, 1528), 'recipes.models.Ingredient.objects.create', 'Ingredient.objects.create', ([], {'recipe': 'recipe'}), '(recipe=recipe, **ingredients)\n', (1498, 1528), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((1967, 2012), 'recipes.models.Step.objects.create', 'Step.objects.create', ([], {'recipe': 'instance'}), '(recipe=instance, **steps)\n', (1986, 2012), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((2071, 2128), 'recipes.models.Ingredient.objects.create', 'Ingredient.objects.create', ([], {'recipe': 'instance'}), '(recipe=instance, **ingredients)\n', (2096, 2128), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((1816, 1852), 'recipes.models.Step.objects.filter', 'Step.objects.filter', ([], {'recipe': 'instance'}), '(recipe=instance)\n', (1835, 1852), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((1870, 1912), 'recipes.models.Ingredient.objects.filter', 'Ingredient.objects.filter', ([], {'recipe': 'instance'}), '(recipe=instance)\n', (1895, 1912), False, 'from recipes.models import Recipe, Ingredient, Step\n'), ((442, 468), 'django.contrib.auth.validators.UnicodeUsernameValidator', 'UnicodeUsernameValidator', ([], {}), '()\n', (466, 468), False, 'from django.contrib.auth.validators import UnicodeUsernameValidator\n')]