// Package packageprice 提供套餐价格与赠送语义的统一判定逻辑
// 负责原始价格、价格状态、生效价和赠送限制的集中计算
package packageprice

import (
	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
)

// ResolvePackagePriceState 解析套餐价格状态和落库存储值。
func ResolvePackagePriceState(isGift bool, rawPrice *int64) (int, int64, error) {
	if isGift {
		if rawPrice == nil || *rawPrice != 0 {
			return 0, 0, errors.New(errors.CodeInvalidParam, "赠送套餐必须显式配置 0 元售价")
		}
		return constants.PackagePriceConfigStatusGiftZero, 0, nil
	}

	if rawPrice == nil {
		return constants.PackagePriceConfigStatusUnconfigured, 0, nil
	}
	if *rawPrice == 0 {
		return 0, 0, errors.New(errors.CodeInvalidParam, "普通可售套餐不允许显式配置 0 元售价")
	}

	return constants.PackagePriceConfigStatusConfigured, *rawPrice, nil
}

// ResolveAllocationPriceState 解析代理分配零售价状态和落库存储值。
func ResolveAllocationPriceState(rawPrice *int64) (int, int64, error) {
	if rawPrice == nil {
		return constants.PackagePriceConfigStatusUnconfigured, 0, nil
	}
	if *rawPrice == 0 {
		return 0, 0, errors.New(errors.CodeInvalidParam, "普通可售套餐不允许显式配置 0 元零售价")
	}

	return constants.PackagePriceConfigStatusConfigured, *rawPrice, nil
}

// PackageEffectiveRetailPrice 返回套餐生效售价。
func PackageEffectiveRetailPrice(pkg *model.Package) int64 {
	if pkg == nil {
		return 0
	}

	switch pkg.PriceConfigStatus {
	case constants.PackagePriceConfigStatusGiftZero:
		return 0
	case constants.PackagePriceConfigStatusConfigured:
		return pkg.SuggestedRetailPrice
	default:
		return pkg.CostPrice
	}
}

// PackageRawSuggestedRetailPrice 返回套餐原始建议售价。
func PackageRawSuggestedRetailPrice(pkg *model.Package) *int64 {
	if pkg == nil || pkg.PriceConfigStatus == constants.PackagePriceConfigStatusUnconfigured {
		return nil
	}
	price := pkg.SuggestedRetailPrice
	return &price
}

// AllocationEffectiveRetailPrice 返回代理分配记录生效零售价。
func AllocationEffectiveRetailPrice(allocation *model.ShopPackageAllocation) int64 {
	if allocation == nil {
		return 0
	}
	if allocation.RetailPriceConfigStatus == constants.PackagePriceConfigStatusUnconfigured {
		return allocation.CostPrice
	}
	return allocation.RetailPrice
}

// AllocationRawRetailPrice 返回代理分配记录原始零售价。
func AllocationRawRetailPrice(allocation *model.ShopPackageAllocation) *int64 {
	if allocation == nil || allocation.RetailPriceConfigStatus == constants.PackagePriceConfigStatusUnconfigured {
		return nil
	}
	price := allocation.RetailPrice
	return &price
}

// ContainsGiftPackage 判断套餐列表中是否包含赠送套餐。
func ContainsGiftPackage(packages []*model.Package) bool {
	for _, pkg := range packages {
		if pkg != nil && pkg.IsGift {
			return true
		}
	}
	return false
}

// ValidateGiftAdminOrder 校验后台赠送订单约束。
func ValidateGiftAdminOrder(packages []*model.Package) error {
	if !ContainsGiftPackage(packages) {
		return nil
	}
	if len(packages) != 1 {
		return errors.New(errors.CodeInvalidParam, "赠送套餐必须单独创建订单")
	}
	for _, pkg := range packages {
		if pkg == nil {
			continue
		}
		if !pkg.IsGift {
			return errors.New(errors.CodeInvalidParam, "赠送订单不能混入普通可售套餐")
		}
	}
	return nil
}
