package packagepkg

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"
)

// ValidateExpiryBaseOverride 校验显式提交的分配生效条件覆盖。
func ValidateExpiryBaseOverride(value *string, submitted bool) (*string, error) {
	if !submitted {
		return nil, errors.New(errors.CodeInvalidParam, "生效条件覆盖字段未提交，须显式传值（null 或合法枚举）")
	}
	if value == nil {
		return nil, nil
	}
	if *value != constants.PackageExpiryBaseFromActivation && *value != constants.PackageExpiryBaseFromPurchase {
		return nil, errors.New(errors.CodeInvalidParam, "生效条件覆盖值非法，合法值为：购买即生效、实名激活时生效")
	}
	return value, nil
}

// EffectiveExpiryBase 返回套餐分配最终采用的生效条件。
func EffectiveExpiryBase(pkg *model.Package, allocation *model.ShopPackageAllocation) string {
	if allocation != nil && allocation.ExpiryBaseOverride != nil {
		return *allocation.ExpiryBaseOverride
	}
	return pkg.ExpiryBase
}

// ExpiryBaseName 返回生效条件中文名称。
func ExpiryBaseName(value string) string {
	switch value {
	case constants.PackageExpiryBaseFromActivation:
		return "实名激活时生效"
	case constants.PackageExpiryBaseFromPurchase:
		return "购买即生效"
	default:
		return "未知"
	}
}

// ExpiryBaseOverrideName 返回覆盖值中文名称，空值表示跟随套餐默认。
func ExpiryBaseOverrideName(value *string) string {
	if value == nil {
		return "跟随套餐默认"
	}
	return ExpiryBaseName(*value)
}
