package tmall

import (
	"context"
	"errors"
	"fmt"
	"kami/utility/integration/tmall"
	"topsdk"
	"topsdk/defaultability"
	"topsdk/defaultability/request"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/duke-git/lancet/v2/retry"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
)

// TradeRatesAdd 商城评价添加
func (c *InnerClient) TradeRatesAdd(ctx context.Context, oid int64) (isSucceed bool, err error) {
	isSucceed = false
	cfg, sessionInfo, err := tmall.GetCfgAndSession(ctx)
	if err != nil {
		return
	}
	if err2 := retry.Retry(func() error {
		cli := topsdk.NewDefaultTopClient(cfg.AppKey, cfg.AppSecret, cfg.Gateway, 1000*60*5, 1000*60*5)
		result, err2 := defaultability.NewDefaultability(&cli).TaobaoTraderateExplainAdd(&request.TaobaoTraderateExplainAddRequest{
			Oid:   gconv.PtrInt64(oid),
			Reply: gconv.PtrString("买家爽快💐💐💐"),
		}, sessionInfo.AccessToken)
		if pointer.IsNil(result) || result.RequestId == "" || err2 != nil {
			err2 = errors.Join(err2, gerror.New("淘宝授权错误"))
		}
		return err2
	}, retry.Context(ctx), retry.RetryTimes(5)); err2 != nil {
		return isSucceed, errors.Join(err, gerror.New("淘宝评价错误"))
	}
	isSucceed = true
	return
}

// SendImageMsg 发送图片消息
func (c *InnerClient) SendImageMsg(ctx context.Context, oid int64) (isSucceed bool, err error) {
	isSucceed = false
	cfg, sessionInfo, err := tmall.GetCfgAndSession(ctx)
	if err != nil {
		return
	}
	if err2 := retry.Retry(func() error {
		cli := topsdk.NewDefaultTopClient(cfg.AppKey, cfg.AppSecret, cfg.Gateway, 1000*60*5, 1000*60*5)
		result, err2 := defaultability.NewDefaultability(&cli).TaobaoTmcMessageProduce(&request.TaobaoTmcMessageProduceRequest{
			Content:      gconv.PtrString(fmt.Sprintf("{\"tid\":2895732958732,\"seller_nick\":\"helloworld\"}")),
			Topic:        nil,
			MediaContent: nil,
		}, sessionInfo.AccessToken)
		if pointer.IsNil(result) || err2 != nil {
			err2 = errors.Join(err, gerror.New("淘宝授权错误"))
		}
		return err2
	}, retry.Context(ctx), retry.RetryTimes(5)); err2 != nil {
		return isSucceed, errors.Join(err, gerror.New("淘宝图片消息错误"))
	}
	isSucceed = true
	return
}
