/** 订单供应商车次统计接口 */
interface IOrderSupplierStatistics {
	/** 自开车次列表 */
	selfInvoiceList: BusinessAPI.OrderSupplierVO[];
	/** 代开车次列表 */
	proxyInvoiceList: BusinessAPI.OrderSupplierVO[];
	/** 无发票车次列表 */
	noInvoiceList: BusinessAPI.OrderSupplierVO[];
	/** 自开车次数量 */
	selfInvoiceCount: number;
	/** 代开车次数量 */
	proxyInvoiceCount: number;
	/** 无发票车次数量 */
	noInvoiceCount: number;
	/** 自开车次金额 */
	selfInvoiceAmount: number;
	/** 代开车次金额 */
	proxyInvoiceAmount: number;
	/** 无发票金额 */
	noInvoiceAmount: number;
	/** 开票方统计 */
	invoicerStatistics: {
		supplierName: string;
		supplierId: string;
		count: number;
		amount: number;
	}[];
}

/** 统计车次数据 */
const calculateOrderSupplierStatistics = (
	orderSupplierList?: BusinessAPI.OrderSupplierVO[],
): IOrderSupplierStatistics => {
	if (!orderSupplierList || orderSupplierList.length === 0) {
		return {
			selfInvoiceList: [],
			proxyInvoiceList: [],
			noInvoiceList: [],
			selfInvoiceCount: 0,
			proxyInvoiceCount: 0,
			noInvoiceCount: 0,
			selfInvoiceAmount: 0,
			proxyInvoiceAmount: 0,
			noInvoiceAmount: 0,
			invoicerStatistics: [],
		};
	}

	const selfInvoiceList: BusinessAPI.OrderSupplierVO[] = [];
	const proxyInvoiceList: BusinessAPI.OrderSupplierVO[] = [];
	const noInvoiceList: BusinessAPI.OrderSupplierVO[] = [];
	const invoicerMap = new Map<
		string,
		{ count: number; amount: number; supplierName: string }
	>();

	for (const orderSupplier of orderSupplierList) {
		const hasInvoice = !!orderSupplier.invoiceId;
		const supplierInvoiceVO = orderSupplier.supplierInvoiceVO;

		if (!hasInvoice) {
			// 无发票车次
			noInvoiceList.push(orderSupplier);
		} else if (
			supplierInvoiceVO &&
			orderSupplier.supplierId === supplierInvoiceVO.supplierId
		) {
			// 自开车次
			selfInvoiceList.push(orderSupplier);
			// 统计开票方 - 使用 supplierId 作为分组键
			const key = supplierInvoiceVO.supplierId;
			const existing = invoicerMap.get(key) || {
				count: 0,
				amount: 0,
				supplierName: supplierInvoiceVO.supplierName,
			};
			invoicerMap.set(key, {
				count: existing.count + 1,
				amount: existing.amount + (orderSupplier.invoiceAmount || 0),
				supplierName: supplierInvoiceVO.supplierName,
			});
		} else {
			// 代开车次
			proxyInvoiceList.push(orderSupplier);
			// 代开也统计开票方 - 使用 supplierId 作为分组键
			if (supplierInvoiceVO) {
				const key = supplierInvoiceVO.supplierId;
				const existing = invoicerMap.get(key) || {
					count: 0,
					amount: 0,
					supplierName: supplierInvoiceVO.supplierName,
				};
				invoicerMap.set(key, {
					count: existing.count + 1,
					amount:
						existing.amount + (orderSupplier.invoiceAmount || 0),
					supplierName: supplierInvoiceVO.supplierName,
				});
			}
		}
	}

	const invoicerStatistics = Array.from(invoicerMap.entries()).map(
		([supplierId, data]) => ({
			supplierId,
			supplierName: data.supplierName,
			count: data.count,
			amount: data.amount,
		}),
	);

	return {
		selfInvoiceList,
		proxyInvoiceList,
		noInvoiceList,
		selfInvoiceCount: selfInvoiceList.length,
		proxyInvoiceCount: proxyInvoiceList.length,
		noInvoiceCount: noInvoiceList.length,
		selfInvoiceAmount: selfInvoiceList.reduce(
			(sum, item) => sum + (item.invoiceAmount || 0),
			0,
		),
		proxyInvoiceAmount: proxyInvoiceList.reduce(
			(sum, item) => sum + (item.invoiceAmount || 0),
			0,
		),
		noInvoiceAmount: noInvoiceList.reduce(
			(sum, item) => sum + (item.invoiceAmount || 0),
			0,
		),
		invoicerStatistics,
	};
};

export { calculateOrderSupplierStatistics };
