package payment

import (
	"errors"
	"fmt"
	"strings"

	"github.com/gin-gonic/gin"

	"tower_api/model"
	"tower_api/pkg/feishu"
	"tower_api/pkg/util"
)

type IPay interface {
	// Callback 支付完成后的回调
	Callback(c *gin.Context) (orderSn string, amount int, err error)
	// Confirm 确认订单是否支付成功
	Confirm() error
	// ReplyContent 回复给上游的相应内容
	ReplyContent(success bool) string
}

// Dispatch 匹配支付方式
func Dispatch(payChannel string) (IPay, error) {
	payChannel = strings.ToUpper(payChannel)
	switch payChannel {
	case "SY":
		return NewSYPay(), nil
	case "8U":
		return NewEightUPay(), nil
	case "QUICK":
		return NewQuickPay(), nil
	default:
		return nil, errors.New("支付方式未找到")
	}
}

// 订单金额不一致飞书通知
func MoneyNotMatchNotify(orderInfo model.Order, price int) {
	content := fmt.Sprintf("[塔防用户订单异常]\nplayer_id：%d\n订单id：%s\n订单支付金额：%d分\n发行侧回调支付金额：%d分\n", orderInfo.PlayerId, orderInfo.OrderSn, orderInfo.OrderPrice, price)
	mod := util.GetRunMod()
	domain := util.Ter(mod == "debug", feishu.OrderErr_Dev, feishu.OrderErr)
	notify := feishu.NewFeiShu(domain)
	notify.BaseNotify(content)
}
