package payment

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

type Factory struct{}

func NewFactory() *Factory { return &Factory{} }

type adapterFactory func() bizpayment.PaymentAdapter

// adapterFactories is the single payment integration registration point.
// Keep constructors zero-state: provider configuration belongs to each call,
// so an adapter can never accidentally retain secrets or order data.
var adapterFactories = map[string]adapterFactory{
	bizpayment.PaymentAlipay:   func() bizpayment.PaymentAdapter { return &alipayAdapter{} },
	bizpayment.PaymentAlipayV3: func() bizpayment.PaymentAdapter { return &alipayV3Adapter{} },
	bizpayment.PaymentWechatV2: func() bizpayment.PaymentAdapter { return &wechatV2Adapter{} },
	bizpayment.PaymentWechatV3: func() bizpayment.PaymentAdapter { return &wechatV3Adapter{} },
	bizpayment.PaymentApple:    func() bizpayment.PaymentAdapter { return &appleAdapter{} },
	bizpayment.PaymentDouyin:   func() bizpayment.PaymentAdapter { return &douyinAdapter{} },
	bizpayment.PaymentQQ:       func() bizpayment.PaymentAdapter { return &qqAdapter{} },
	bizpayment.PaymentAllinPay: func() bizpayment.PaymentAdapter { return &allinpayAdapter{} },
	bizpayment.PaymentLakala:   func() bizpayment.PaymentAdapter { return &lakalaAdapter{} },
	bizpayment.PaymentPayPal:   func() bizpayment.PaymentAdapter { return &paypalAdapter{} },
	bizpayment.PaymentSaobei:   func() bizpayment.PaymentAdapter { return &saobeiAdapter{} },
	bizpayment.PaymentChinaums: func() bizpayment.PaymentAdapter { return newVendorAdapter(bizpayment.PaymentChinaums, vendorChinaums) },
	bizpayment.PaymentSFT:      func() bizpayment.PaymentAdapter { return newVendorAdapter(bizpayment.PaymentSFT, vendorSFT) },
	bizpayment.PaymentSuperPay: func() bizpayment.PaymentAdapter { return newVendorAdapter(bizpayment.PaymentSuperPay, vendorSupperPay) },
	bizpayment.PaymentWechatGame: func() bizpayment.PaymentAdapter {
		return newVendorAdapter(bizpayment.PaymentWechatGame, vendorWechatGame)
	},
	bizpayment.PaymentDouyinGame: func() bizpayment.PaymentAdapter {
		return newVendorAdapter(bizpayment.PaymentDouyinGame, vendorDouyinGame)
	},
}

// New constructs the SDK-backed adapter for a configured provider. Provider
// identifiers are normalized at this I/O boundary so direct callers get the
// same behavior as the configuration and business layers.
func (*Factory) New(provider string) (bizpayment.PaymentAdapter, error) {
	provider = strings.ToLower(strings.TrimSpace(provider))
	factory, ok := adapterFactories[provider]
	if !ok {
		return nil, fmt.Errorf("支付渠道 %s 没有适配器", provider)
	}
	return factory(), nil
}
