package whatsapp

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

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

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

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

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

func (l *GetAutomationLogic) GetAutomation(req *types.AutomationReq) (*types.AutomationResp, error) {
	resp := types.AutomationResp{}
	automation, err := aliyun.GetAutomation(*req.WaId, *req.Phone)
	l.Logger.Infof("automation=%v err=%v\n", automation, err)
	if err != nil {
		resp.Msg = err.Error()
		resp.Code = 1
	}

	// 获取内容并赋值
	if automation.Body.Data.Commands != nil {
		for _, v := range automation.Body.Data.Commands {
			resp.Data.Commands = append(resp.Data.Commands, types.Command{
				CommandDescription: *v.CommandDescription,
				CommandName:        *v.CommandName,
			})
		}
	}
	if automation.Body.Data.Prompts != nil {
		for _, v := range automation.Body.Data.Prompts {
			resp.Data.Prompts = append(resp.Data.Prompts, *v)
		}
	}
	if automation.Body.Data.PhoneNumber != nil {
		resp.Data.Phone = *automation.Body.Data.PhoneNumber
	}

	if automation.Body.Data.EnableWelcomeMessage != nil {
		resp.Data.EnableWelcomeMessage = *automation.Body.Data.EnableWelcomeMessage
	}

	return &resp, nil
}
