from rest_framework import serializers

from shop_management.models.data.stock_transfer import StockTransfer
from shop_management.serializers.data.base import DataRootSerializer


class StockTransferSerializer(DataRootSerializer):
    status_display = serializers.CharField(source="get_status_display", read_only=True)
    product_details = serializers.SerializerMethodField()
    currency_details = serializers.SerializerMethodField()
    source_stock_details = serializers.SerializerMethodField()
    target_stock_details = serializers.SerializerMethodField()
    supplier_branch_details = serializers.SerializerMethodField()
    customer_branch_details = serializers.SerializerMethodField()
    accepted_by_details = serializers.SerializerMethodField()
    transfer_user_details = serializers.SerializerMethodField()

    class Meta:
        model = StockTransfer
        fields = "__all__"

    def get_product_details(self, obj):
        return {"id": obj.product_id, "name": obj.product.name} if obj.product else None

    def get_currency_details(self, obj):
        currency = obj.currency
        if currency:
            return {"id": currency.id, "title": currency.title}
        return None

    def _stock_payload(self, stock):
        if not stock or not stock.purchase_item:
            return None
        product = stock.purchase_item.product
        unit = stock.unit
        currency = stock.currency
        return {
            "id": stock.id,
            "shop": {"id": stock.shop_id, "name": stock.shop.name} if stock.shop else None,
            "product": {
                "id": product.id if product else None,
                "name": product.name if product else None,
                "code": product.code if product else None,
            },
            "buy_price": str(stock.purchase_item.buy_price) if stock.purchase_item.buy_price is not None else None,
            "wholesale_price": str(stock.wholesale_price) if stock.wholesale_price is not None else None,
            "retail_price": str(stock.retail_price) if stock.retail_price is not None else None,
            "quantity": stock.quantity,
            "unit": {"id": unit.id, "title": unit.title} if unit else None,
            "currency_details": {"id": currency.id, "title": currency.title} if currency else None,
        }

    def get_source_stock_details(self, obj):
        return self._stock_payload(obj.source_stock)

    def get_target_stock_details(self, obj):
        return self._stock_payload(obj.target_stock)

    def get_supplier_branch_details(self, obj):
        return {"id": obj.supplier_branch_id, "name": obj.supplier_branch.name} if obj.supplier_branch else None

    def get_customer_branch_details(self, obj):
        return {"id": obj.customer_branch_id, "name": obj.customer_branch.name} if obj.customer_branch else None

    def get_accepted_by_details(self, obj):
        user = obj.accepted_by
        if not user:
            return None
        return {"id": user.id, "username": user.username, "name": getattr(user, "get_full_name", lambda: "")() or user.username}

    def get_transfer_user_details(self, obj):
        user = obj.created_by
        if not user:
            return None
        return {"id": user.id, "username": user.username, "name": getattr(user, "get_full_name", lambda: "")() or user.username}
