package test

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	marketing2 "voucher/internal/pkg/wechat/srv/marketing"
	"voucher/internal/pkg/wechat/utils"
)

func marketing() *marketing2.Marketing {

	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	parentDir := filepath.Dir(dir)

	//openssl x509 -in xxx.pem -noout -serial

	mchId := "1710953361"
	wechatPayPublicKeyId := "PUB_KEY_ID_0117109533612025031800326400002563"
	certificateSerialNo := "6006B8208815DB5EAC5BF2E783CB9D34082C3772"

	//mchId := "1652465541"
	//wechatPayPublicKeyId := ""
	//certificateSerialNo := "1E3F2CE013203BA9C3DEFC5782FCD3329C3DAC1C"

	filePath := fmt.Sprintf("%s/cert/wechat/%s", parentDir, mchId)

	c, err := utils.CreateMchConfig(
		mchId,               // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		certificateSerialNo, // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"), // 商户API证书私钥文件路径，本地文件路径
		wechatPayPublicKeyId,                          // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		fmt.Sprintf("%s/%s", filePath, "pub_key.pem"), // 微信支付公钥文件路径，本地文件路径
		"",
	)
	if err != nil {
		panic(err)
	}

	return &marketing2.Marketing{MchConfig: c}
}

func marketingFJxw() *marketing2.Marketing {

	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	parentDir := filepath.Dir(dir)

	//openssl x509 -in xxx.pem -noout -serial

	mchId := "1652465541"
	wechatPayPublicKeyId := " PUB_KEY_ID_0116524655412025070900181741001803"
	certificateSerialNo := "1E3F2CE013203BA9C3DEFC5782FCD3329C3DAC1C"

	filePath := fmt.Sprintf("%s/cert/wechat/%s", parentDir, mchId)

	c, err := utils.CreateMchConfig(
		mchId,               // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		certificateSerialNo, // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"), // 商户API证书私钥文件路径，本地文件路径
		wechatPayPublicKeyId,                          // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		fmt.Sprintf("%s/%s", filePath, "pub_key.pem"), // 微信支付公钥文件路径，本地文件路径
		"",
	)
	if err != nil {
		panic(err)
	}

	return &marketing2.Marketing{MchConfig: c}
}

func marketingFJLF() *marketing2.Marketing {

	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	parentDir := filepath.Dir(dir)

	mchId := "1100040695"
	wechatPayPublicKeyId := "PUB_KEY_ID_0111000406952026032500382251001000"
	certificateSerialNo := "46712853869DB0EDAA9B4DF97DADEECD4CCDC85B"

	filePath := fmt.Sprintf("%s/cert/wechat/%s", parentDir, mchId)
	fmt.Printf("filePath: %s\n", filePath)

	c, err := utils.CreateMchConfig(
		mchId,               // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		certificateSerialNo, // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"), // 商户API证书私钥文件路径，本地文件路径
		wechatPayPublicKeyId,                          // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		fmt.Sprintf("%s/%s", filePath, "pub_key.pem"), // 微信支付公钥文件路径，本地文件路径
		"d9af70585b18ae206d981548c766563f",
	)
	if err != nil {
		panic(err)
	}

	return &marketing2.Marketing{MchConfig: c}
}

func MarketingSend() {

	openId := "oSNb4ftgnWC22Z0cWTjsQebdr2Yk"
	appId := "wx619991cc795028f5"

	request := &marketing2.SendReq{
		ActivityId:        utils.String("11941580000000012"),
		StockId:           utils.String("20964154"),
		OutRequestNo:      utils.String("196605139814767820814"),
		Appid:             utils.String(appId),
		StockCreatorMchId: utils.String("1652465541"),
	}

	response, err := marketing().Send(openId, request)
	if err != nil {

		var e *utils.ApiException
		if errors.As(err, &e) {

			apiErr, err3 := marketing2.BuildErr(e.Body())
			if err3 != nil {
				fmt.Printf("请求失败: %+v\n", err3)
			} else {
				fmt.Printf("请求失败: %s-%s\n", apiErr.Code, apiErr.Message)
			}

		}

		return
	}

	fmt.Printf("请求成功: %+v\n", response)
	fmt.Printf("请求成功: %s\n", *response.CouponId)
}

func MarketingQuery(appId, openId, couponId string) (response *marketing2.SendResp, err error) {
	return marketingFJxw().Query(appId, openId, couponId)
	//return marketingFJLF().Query(appId, openId, couponId)
}
