package payment

import (
	"errors"
	"fmt"
	bizpayment "kra/internal/biz/payment"
	"strings"

	"kra/internal/paymentkit"
)

func callbackFields(callback *bizpayment.PaymentCallback) map[string]string {
	if callback == nil {
		return map[string]string{}
	}
	return paymentkit.CallbackFields(callback.Query, callback.Headers, callback.Body)
}

func first(values map[string]string, keys ...string) string {
	return paymentkit.FirstString(values, keys...)
}

func firstNonEmpty(values ...string) string {
	for _, value := range values {
		if value = strings.TrimSpace(value); value != "" && value != "<nil>" {
			return value
		}
	}
	return ""
}

func text(values map[string]any, key string) string {
	return paymentkit.Text(values, key)
}

func firstAny(values map[string]any, keys ...string) string {
	return paymentkit.FirstText(values, keys...)
}

// Compatibility shims keep channel adapters focused on protocol and
// persistence concerns while stateless parsing rules live in paymentkit.
func normalizePaymentStatus(value, fallback string) string {
	return paymentkit.NormalizeStatus(value, fallback)
}

func normalizeRefundStatus(value, fallback string) string {
	return paymentkit.NormalizeRefundStatus(value, fallback)
}

func parseIntegerAmount(value string) (int64, error) {
	return paymentkit.ParseIntegerAmount(value)
}

func parseDecimalAmount(value string, scale int64) (int64, error) {
	return paymentkit.ParseDecimalAmount(value, scale)
}

func formatDecimalAmount(amount, scale int64) (string, error) {
	return paymentkit.FormatDecimalAmount(amount, scale)
}

func configuredInt64(values map[string]any, key string, fallback int64) int64 {
	return paymentkit.ConfiguredInt64(values, key, fallback)
}

func jsonObject(raw []byte) map[string]any {
	return paymentkit.JSONObject(raw)
}

func valueAtPath(value any, path string) any {
	return paymentkit.ValueAtPath(value, path)
}

func stringAtPath(value any, path string) string {
	return paymentkit.StringAtPath(value, path)
}

func configuredValues(values map[string]any, key string) []string {
	return paymentkit.ConfiguredValues(values, key)
}

func containsFold(values []string, value string) bool {
	return paymentkit.ContainsFold(values, value)
}

// parseConfiguredAmount reads a provider amount field using an explicit unit
// conversion. Missing fields are reported separately so callers can avoid
// manufacturing a complete breakdown from incomplete provider responses.
func parseConfiguredAmount(object any, config map[string]any, fieldKey, scaleKey string, fallbackScale int64) (int64, bool, error) {
	path := strings.TrimSpace(text(config, fieldKey))
	if path == "" {
		return 0, false, nil
	}
	raw := valueAtPath(object, path)
	if raw == nil || strings.TrimSpace(fmt.Sprint(raw)) == "" || strings.EqualFold(strings.TrimSpace(fmt.Sprint(raw)), "<nil>") {
		return 0, false, nil
	}
	value := strings.TrimSpace(fmt.Sprint(raw))
	scale := configuredInt64(config, scaleKey, fallbackScale)
	if scale <= 0 {
		return 0, false, errors.New("支付金额换算比例无效")
	}
	amount, err := parseIntegerAmount(value)
	if scale != 1 {
		amount, err = parseDecimalAmount(value, scale)
	}
	if err != nil {
		return 0, true, err
	}
	if amount < 0 {
		return 0, true, errors.New("支付金额不能为负数")
	}
	return amount, true, nil
}
