from apps.pos_device.models import Device
from apps.product.models import Quota
import typing


def pos_organizations_sharing_information(device: Device, quota: Quota = None) -> typing.Any:
    """
    pos sharing organizations' information,
    device have multiple organizations (sub_accounts) for sharing money
    """
    stake_holders = device.stake_holders.select_related('broker', 'broker_amount', 'organization')

    sharing_information_list = [{
        "organization_name": item.organization.name,
        "bank_account": {
            "credit_card": item.organization.bank_information.first().card,
            "sheba": item.organization.bank_information.first().sheba,
            "account": item.organization.bank_information.first().account,
        } if item.organization.bank_information.exists() else {},
        "broker": item.broker.name if item.broker else None,
        "amount": quota.broker_values.filter(
            broker=item.broker
        ).first().value if quota and item.broker else item.share_amount
    } for item in stake_holders]

    return sharing_information_list
