package mysql

import (
	"fmt"
	"hyapp/pkg/appcode"
	"hyapp/services/wallet-service/internal/domain/ledger"
	"strings"
)

func giftDebitBizType(command ledger.DebitGiftCommand) string {
	if command.RobotGift {
		return bizTypeRobotGiftDebit
	}
	if command.DirectGift {
		return bizTypeDirectGiftDebit
	}
	return bizTypeGiftDebit
}

func aggregateGiftReceipts(targets []ledger.BatchGiftTargetReceipt) (ledger.Receipt, error) {
	aggregate := ledger.Receipt{}
	billingReceiptIDs := make([]string, 0, len(targets))
	transactionIDs := make([]string, 0, len(targets))
	for _, target := range targets {
		receipt := target.Receipt
		if receipt.BillingReceiptID != "" {
			billingReceiptIDs = append(billingReceiptIDs, receipt.BillingReceiptID)
		}
		if receipt.TransactionID != "" {
			transactionIDs = append(transactionIDs, receipt.TransactionID)
		}
		var err error
		if aggregate.CoinSpent, err = checkedAdd(aggregate.CoinSpent, receipt.CoinSpent); err != nil {
			return ledger.Receipt{}, err
		}
		if aggregate.ChargeAmount, err = checkedAdd(aggregate.ChargeAmount, receipt.ChargeAmount); err != nil {
			return ledger.Receipt{}, err
		}
		if aggregate.HeatValue, err = checkedAdd(aggregate.HeatValue, receipt.HeatValue); err != nil {
			return ledger.Receipt{}, err
		}
		if aggregate.HostPeriodDiamondAdded, err = checkedAdd(aggregate.HostPeriodDiamondAdded, receipt.HostPeriodDiamondAdded); err != nil {
			return ledger.Receipt{}, err
		}
		if aggregate.ChargeAssetType == "" {
			aggregate.ChargeAssetType = receipt.ChargeAssetType
		} else if receipt.ChargeAssetType != "" && aggregate.ChargeAssetType != receipt.ChargeAssetType {
			aggregate.ChargeAssetType = giftWallChargeAssetMixed
		}
		if aggregate.GiftTypeCode == "" {
			aggregate.GiftTypeCode = receipt.GiftTypeCode
		}
		if aggregate.CPRelationType == "" {
			aggregate.CPRelationType = receipt.CPRelationType
		}
		if aggregate.GiftName == "" {
			aggregate.GiftName = receipt.GiftName
		}
		if aggregate.GiftIconURL == "" {
			aggregate.GiftIconURL = receipt.GiftIconURL
		}
		if aggregate.GiftAnimationURL == "" {
			aggregate.GiftAnimationURL = receipt.GiftAnimationURL
		}
		if aggregate.PriceVersion == "" {
			aggregate.PriceVersion = receipt.PriceVersion
		}
		if aggregate.HostPeriodCycleKey == "" {
			aggregate.HostPeriodCycleKey = receipt.HostPeriodCycleKey
		}

		aggregate.BalanceAfter = receipt.BalanceAfter
	}
	aggregate.BillingReceiptID = strings.Join(billingReceiptIDs, ",")
	aggregate.TransactionID = strings.Join(transactionIDs, ",")
	return aggregate, nil
}

func debitRequestHash(command ledger.DebitGiftCommand) string {

	if command.DirectGift {
		// direct gift 没有 Room Cell 命令，必须把场景写进幂等摘要，避免和空 room_id 的内部兼容调用互相复用。
		return stableHash(fmt.Sprintf("direct_gift|%s|%s|%d|%d|%s|%d|%s|%d|%d|%t|%d|%d|%t|%s|%s",
			appcode.Normalize(command.AppCode),
			command.RoomID,
			command.SenderUserID,
			command.TargetUserID,
			command.GiftID,
			command.GiftCount,
			strings.TrimSpace(command.PriceVersion),
			command.RegionID,
			command.SenderRegionID,
			command.TargetIsHost,
			command.TargetHostRegionID,
			command.TargetAgencyOwnerUserID,
			command.RobotGift,
			strings.TrimSpace(command.EntitlementID),
			normalizeGiftChargeSource(command.ChargeSource, command.EntitlementID),
		))
	}

	return stableHash(fmt.Sprintf("gift|%s|%s|%d|%d|%s|%d|%s|%d|%d|%t|%d|%d|%t|%s|%s",
		appcode.Normalize(command.AppCode),
		command.RoomID,
		command.SenderUserID,
		command.TargetUserID,
		command.GiftID,
		command.GiftCount,
		strings.TrimSpace(command.PriceVersion),
		command.RegionID,
		command.SenderRegionID,
		command.TargetIsHost,
		command.TargetHostRegionID,
		command.TargetAgencyOwnerUserID,
		command.RobotGift,
		strings.TrimSpace(command.EntitlementID),
		normalizeGiftChargeSource(command.ChargeSource, command.EntitlementID),
	))
}
