"""
Party serializer
"""

from django.utils.translation import gettext as _
from rest_framework import serializers

from core.apps.eggs import models
from core.apps.eggs.serializers import group
from core.apps.eggs.serializers import group as group_serializer
from core.apps.eggs.serializers import invoice
from core.http.serializers import user


class PartySerializer(serializers.ModelSerializer):
    invoices = invoice.InvoiceSerializer(many=True)
    groups = group.GroupSerializer(many=True)
    user = user.UserSerializer(read_only=True, source="user_id")

    class Meta:
        model = models.Party
        fields = (
            "id",
            "user",
            "count",
            "sold_count",
            "remaining_count",
            "profit",
            "benefit",
            "cost",
            "total_cost",
            "created_at",
            "invoices",
            "groups",
            "broken_eggs",
            "courier_eggs",
        )

    def create(self, validated_data):
        invoices_data = validated_data.pop("invoices", [])
        groups_data = validated_data.pop("groups", [])
        party = models.Party.objects.create(**validated_data)

        for invoice_data in invoices_data:
            invoice_data["party_id"] = party
            models.Invoice.objects.create(**invoice_data)

        for group_data in groups_data:
            group_data["party_id"] = party
            models.Group.objects.create(**group_data)

        return party


class PartyGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Group
        fields = (
            "id",
            "name",
            "entry_price",
            "unit_price",
            "wholesale_price",
            "quantity",
            "broken_eggs",
            "product_id",
        )


class PartyCreateSerializer(serializers.ModelSerializer):
    invoices = invoice.InvoiceSerializer(many=True)
    groups = PartyGroupSerializer(many=True)

    class Meta:
        model = models.Party
        fields = (
            "invoices",
            "groups",
        )

    def create(self, validated_data):
        user = self.context["request"].user
        invoices_data = validated_data.pop("invoices", [])
        groups_data = validated_data.pop("groups", [])
        party = models.Party.objects.create(user_id=user, **validated_data)

        for invoice_data in invoices_data:
            invoice_data["party_id"] = party
            models.Invoice.objects.create(**invoice_data)

        created_groups = []
        for group_data in groups_data:
            group_data["party_id"] = party
            created_group = models.Group.objects.create(**group_data)
            created_groups.append(created_group)

        party.groups.set(created_groups)

        full_name = f"{user.first_name} {user.last_name}"

        models.History.objects.create(
            content_object=party,
            action="party_created",
            user_id=user,
            created_who=full_name,
            created_by=party.groups.name,
            reason=_(f"Partiya yaratildi: {party.total_cost}"),
            comment=_(f"Partiya yaratildi: {party.total_cost}"),
            avatar=user.avatar if user.avatar else None,
        )
        models.Monitoring.objects.create(
            content_object=party,
            action="party_created",
            user_id=user,
            created_who=full_name,
            created_by=party.groups.name,
            reason=_(f"Partiya yaratildi: {party.total_cost}"),
            comment="Chiqim",
            price=party.total_cost,
        )

        return party

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation["groups"] = group_serializer.GroupSerializer(
            instance.groups.all(), many=True
        ).data

        return representation
