from apps.product.models import Quota, QuotaLivestockAllocation
from apps.warehouse.models import InventoryEntry
import typing


def get_products_in_warehouse(organization_id):
    """ get list of products from organization warehouse """

    entries = InventoryEntry.objects.select_related(
        'distribution__quota__product'
    )

    product_objects = [
        entry.distribution.quota.product for entry in entries
    ]

    return list(set(product_objects))


def quota_live_stock_allocation_info(quota: Quota) -> typing.Any:
    """ information of quota live stock allocations """

    allocations = quota.livestock_allocations.select_related('livestock_type')

    if allocations:
        allocations_list = [{
            "name": alloc.livestock_type.name,
            "quantity": alloc.quantity_kg,
            "type": alloc.livestock_type.weight_type
        } for alloc in allocations]

        return allocations_list


def quota_incentive_plans_info(quota: Quota) -> typing.Any:
    """ information of quota incentive plans """

    incentive_plans = quota.incentive_assignments.select_related("livestock_type", "incentive_plan")

    if incentive_plans:
        incentive_plans_list = [{
            'name': plan.incentive_plan.name,
            'heavy_value': plan.heavy_value,
            'light_value': plan.light_value,
            'livestock_type': plan.livestock_type.name if plan.livestock_type else "",
            'livestock_weight_type': plan.livestock_type.weight_type if plan.livestock_type else "",
            'quantity_kg': plan.quantity_kg
        } for plan in incentive_plans]

        return incentive_plans_list


def quota_brokers_value(quota: Quota) -> typing.Any:
    """ information of quota brokers with their quota value """

    brokers = quota.broker_values.select_related("broker")

    if brokers:
        broker_values_list = [{
            'name': broker.broker.name,
            'amount': broker.value
        } for broker in brokers]

        return broker_values_list


def quota_attribute_value(quota: Quota) -> typing.Any:
    """ information of quota pricing attribute values """

    attributes = quota.attribute_values.select_related("attribute")

    if attributes:
        attribute_values_list = [{
            'name': attr.attribute.name,
            'amount': attr.value
        } for attr in attributes]

        return attribute_values_list
