package whatsapp

import (
	"context"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/hook/aliyun"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetWhatsappTemplateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetWhatsappTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhatsappTemplateLogic {
	return &GetWhatsappTemplateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetWhatsappTemplateLogic) GetWhatsappTemplate(req *types.GetTemplateReq) (*types.GetTemplateResp, error) {
	resp := types.GetTemplateResp{}

	if req.Language == "" {
		return nil, errorx.NewInvalidArgumentError("language参数错误")
	}

	result, err := aliyun.GetChatappTemplateInfo(req.TemplateName, req.Language, req.TemplateType, req.TemplateCode)
	l.Logger.Infof("result=%v err=%v\n", result, err)

	if err != nil {
		resp.Msg = err.Error()
		resp.Code = 1
	} else {
		if *result.Body.Code == "OK" && result.Body.Data != nil {
			resp.Data = types.TemplateSingleInfo{
				AuditStatus:  *result.Body.Data.AuditStatus,
				Category:     *result.Body.Data.Category,
				Language:     *result.Body.Data.Language,
				Name:         *result.Body.Data.Name,
				TemplateCode: *result.Body.Data.TemplateCode,
				TemplateType: *result.Body.Data.TemplateType,
			}
		}
	}

	return &resp, nil
}
