package payment

import (
	"context"
	"fmt"

	"kra/internal/biz"
)

// Adapter is the provider boundary used by the payment repository. Provider
// SDK types stay in this package and are normalized to biz.PaymentResult.
type Adapter interface {
	Create(context.Context, *biz.PaymentRequest, map[string]any) (*biz.PaymentResult, error)
	Query(context.Context, string, map[string]any) (*biz.PaymentResult, error)
	Refund(context.Context, *biz.PaymentRefundRequest, map[string]any) (*biz.PaymentResult, error)
	Callback(context.Context, *biz.PaymentCallback, map[string]any) (*biz.PaymentResult, error)
}

// New constructs the SDK-backed adapter for a configured provider.
func New(provider string) (Adapter, error) {
	switch provider {
	case biz.PaymentAlipay:
		return &alipayAdapter{}, nil
	case biz.PaymentAlipayV3:
		return &alipayV3Adapter{}, nil
	case biz.PaymentWechatV2:
		return &wechatV2Adapter{}, nil
	case biz.PaymentWechatV3:
		return &wechatV3Adapter{}, nil
	case biz.PaymentApple:
		return &appleAdapter{}, nil
	case biz.PaymentDouyin:
		return &douyinAdapter{}, nil
	case biz.PaymentQQ:
		return &qqAdapter{}, nil
	case biz.PaymentAllinPay:
		return &allinpayAdapter{}, nil
	case biz.PaymentLakala:
		return &lakalaAdapter{}, nil
	case biz.PaymentPayPal:
		return &paypalAdapter{}, nil
	case biz.PaymentSaobei:
		return &saobeiAdapter{}, nil
	case biz.PaymentChinaums:
		return newVendorAdapter(provider, vendorChinaums), nil
	case biz.PaymentSFT:
		return newVendorAdapter(provider, vendorSFT), nil
	case biz.PaymentSuperPay:
		return newVendorAdapter(provider, vendorSupperPay), nil
	case biz.PaymentWechatGame:
		return newVendorAdapter(provider, vendorWechatGame), nil
	case biz.PaymentDouyinGame:
		return newVendorAdapter(provider, vendorDouyinGame), nil
	default:
		return nil, fmt.Errorf("支付渠道 %s 没有适配器", provider)
	}
}
