package whatsapp

import (
	"context"
	"wechat-api/hook/aliyun"

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

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

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

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

func (l *ListWhatsappTemplateLogic) ListWhatsappTemplate(req *types.ListTemplateReq) (*types.ListTemplateResp, error) {
	resp := types.ListTemplateResp{}
	result, err := aliyun.ListChatappTemplate(req.WaId, req.Name, req.Language, req.AuditStatus,
		req.TemplateType, req.Code, req.Page, req.PageSize)
	l.Logger.Infof("result=%v err=%v\n", result, err)

	if err != nil {
		resp.Msg = err.Error()
		resp.Code = 1
	} else {
		resp.Code = 0
		resp.Data.Total = uint64(*result.Body.Total)
		if len(result.Body.ListTemplate) > 0 && *result.Body.Code == "OK" {
			for _, val := range result.Body.ListTemplate {
				resp.Data.Data = append(resp.Data.Data, types.TemplateSingleInfo{
					AuditStatus:  *val.AuditStatus,
					Category:     *val.Category,
					Language:     *val.Language,
					TemplateCode: *val.TemplateCode,
					Name:         *val.TemplateName,
					TemplateType: *val.TemplateType,
				})
			}
		}
	}

	return &resp, nil
}