from rest_framework import serializers

from LiveStock.models import Cooperative, LiveStockRolseProduct
from authentication.serializer.serializer import BankCardSerializer, SystemUserProfileForInspectionSerializer
from authentication.serializers import SystemAddressSerializer
from ticket.serializers import SystemUserProfileForTicketPermissionSerializer


class CooperativeSerializer(serializers.ModelSerializer):
    user = SystemUserProfileForInspectionSerializer(read_only=True)
    address = SystemAddressSerializer(read_only=True)
    user_bank_info = BankCardSerializer(required=False)

    class Meta:
        model = Cooperative
        fields = '__all__'


class CooperativeForSharesSerializer(serializers.ModelSerializer):
    user = SystemUserProfileForInspectionSerializer(read_only=True)

    class Meta:
        model = Cooperative
        fields = ['user', 'name']


class CooperativeForAllocationsReportSerializer(serializers.ModelSerializer):
    user = SystemUserProfileForInspectionSerializer(read_only=True)
    info = serializers.SerializerMethodField('get_info')

    class Meta:
        model = Cooperative
        fields = ['key','user', 'name','info']

    def get_info(self, obj):
        products = {
            "bran":"سبوس",
            "barley":"جو",
            "soy":"سویا",
            "corn":"ذرت",
            "sheep_concentrate":"کنسانتره گوسفندی",
            "high_cow_concentrate":"کنسانتره گاو شیری پرتولید",
            "medium_cow_concentrate":"کنسانتره گاو شیری متوسط",
            "fattening_calf_concentrate":"کنسانتره گوساله پرواری",
        }
        product =products[self.context.get('request').GET['name']]
        cooperative_roles = LiveStockRolseProduct.objects.get(cooperative=obj,parent_product__name=product)
        total_weight = cooperative_roles.total_weight
        total_receipt_weight = cooperative_roles.total_receipt_weight
        total_allocated_weight = cooperative_roles.total_allocated_weight
        total_remain_weight = cooperative_roles.total_remain_weight
        return {
            "total_weight": total_weight,
            "total_receipt_weight": total_receipt_weight,
            "total_allocated_weight": total_allocated_weight,
            "total_remain_weight": total_remain_weight,
        }


class CooperativeForPosSerializer(serializers.ModelSerializer):
    user = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    address = SystemAddressSerializer(read_only=True)

    class Meta:
        model = Cooperative
        fields = ['user', 'name', 'address']
