package data

import (
	"context"
	"fmt"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
	"testing"
)

func TestGetClient(t *testing.T) {
	server := Server{
		MchID:                      "1605446142", // 证书所属商户 蓝色兄弟服务商立减金配置
		MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
	}
	stockId := ""
	stockCreatorMchid := ""

	ctx := context.Background()

	client, err := GetClient(ctx, server)
	if err != nil {
		t.Error(err)
		return
	}

	svc := cashcoupons.StockApiService{Client: client}

	response, result, err := svc.QueryStock(ctx, cashcoupons.QueryStockRequest{
		StockId:           core.String(stockId),
		StockCreatorMchid: core.String(stockCreatorMchid),
	})

	if err != nil {
		t.Error(err)
		return
	}

	if result.Response.StatusCode != 200 {
		err = fmt.Errorf("查询活动微信返回错误StatusCode[%d]Status[%s]", result.Response.StatusCode, result.Response.Status)
		t.Error(err)
		return
	}

	t.Log(response)
}

func TestCoupon(t *testing.T) {

	server := Server{
		MchID:                      "1710953361", // 证书所属商户 蓝色兄弟服务商立减金配置
		MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772",
	}

	ctx := context.Background()

	client, err := GetClient(ctx, server)
	if err != nil {
		t.Error(err)
		return
	}

	req := cashcoupons.SendCouponRequest{
		OutRequestNo: core.String("123456"),
		// 微信为发券方商户分配的公众账号ID，接口传入的所有appid应该为公众号的appid（在mp.weixin.qq.com申请的），不能为APP的appid（在open.weixin.qq.com申请的）。
		Appid:             core.String("wx619991cc795028f5"),
		Openid:            core.String("oSNb4ftgnWC22Z0cWTjsQebdr2Yk"),
		StockId:           core.String("20395589"),
		StockCreatorMchid: core.String("1652465541"),
	}

	svc := cashcoupons.CouponApiService{Client: client}

	resp, result, err := svc.SendCoupon(ctx, req)

	if err != nil {
		t.Error(err)
		return
	}

	t.Logf("\nresp:%+v\n result:%+v", resp, result)
	return
}
