package jobs

import (
	"context"
	"encoding/json"
	"testing"

	"quyun/app/models"
	"quyun/app/service/testx"

	"github.com/go-pay/gopay/wechat/v3"
	. "github.com/riverqueue/river"
	. "github.com/smartystreets/goconvey/convey"
	"github.com/stretchr/testify/suite"
	_ "go.ipao.vip/atom"
	"go.ipao.vip/atom/contracts"
	"go.uber.org/dig"
)

type WechatPayNotifySuiteInjectParams struct {
	dig.In

	Initials []contracts.Initial `group:"initials"` // nolint:structcheck
}

type WechatPayNotifySuite struct {
	suite.Suite

	WechatPayNotifySuiteInjectParams
}

func Test_WechatPayNotify(t *testing.T) {
	providers := testx.Default().With(Provide, models.Provide)

	testx.Serve(providers, t, func(p WechatPayNotifySuiteInjectParams) {
		suite.Run(t, &WechatPayNotifySuite{WechatPayNotifySuiteInjectParams: p})
	})
}

func (t *WechatPayNotifySuite) Test_Work() {
	Convey("test_work", t.T(), func() {
		Convey("step 1", func() {
			notify := `{ "mchid": "1702644947", "appid": "wx47649361b6eba174", "out_trade_no": "20250430192543", "transaction_id": "4200002602202504300651871941", "trade_type": "JSAPI", "trade_state": "SUCCESS", "trade_state_desc": "支付成功", "bank_type": "OTHERS", "attach": "", "success_time": "2025-04-30T19:25:51+08:00", "payer": { "openid": "o5Bzk644x3LOMJsKSZRlqWin74IU" }, "amount": { "total": 1, "payer_total": 1, "currency": "CNY", "payer_currency": "CNY" } }`

			var payNotify wechat.V3DecryptPayResult
			err := json.Unmarshal([]byte(notify), &payNotify)
			So(err, ShouldBeNil)

			job := &Job[WechatPayNotify]{
				Args: WechatPayNotify{
					Notify: &payNotify,
				},
			}

			worker := &WechatPayNotifyWorker{}

			err = worker.Work(context.Background(), job)
			So(err, ShouldBeNil)
		})
	})
}
