package sop_stage

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/errorx"
	"net/http"
	"wechat-api/ent/sopstage"
	"wechat-api/hook"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *GetApiSopStageListLogic) GetApiSopStageList(req *types.BatchSopStageListReq, r *http.Request) (*types.SopStageListResp, error) {
	tokenStr := r.Header.Get("Authorization")
	_, err := hook.CheckDesktopAuth(tokenStr, l.svcCtx.DB)
	if err != nil {
		return nil, errorx.NewCodeInvalidArgumentError("check auth failed")
	}

	data, err := l.svcCtx.DB.SopStage.Query().Where(sopstage.TaskIDIn(req.TaskIds...)).All(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	resp := &types.SopStageListResp{}
	resp.Msg = errormsg.Success
	resp.Data.Total = uint64(len(data))

	for _, v := range data {
		var conditionList []types.Condition
		if len(v.ConditionList) > 0 {
			conditionList = make([]types.Condition, len(v.ConditionList))
			for i, condition := range v.ConditionList {
				conditionList[i] = types.Condition{
					Equal:       condition.Equal,
					LabelIdList: condition.LabelIdList,
				}
			}
		}

		var actionMessage []types.Action
		if len(v.ActionMessage) > 0 {
			actionMessage = make([]types.Action, len(v.ActionMessage))
			for i, condition := range v.ActionMessage {
				actionMessage[i] = types.Action{
					Type:    condition.Type,
					Content: condition.Content,
				}
			}
		}

		var actionForward *types.ActionForward
		if v.ActionForward != nil {
			actionForward = &types.ActionForward{}
			actionForward.Wxid = v.ActionForward.Wxid
			if len(v.ActionForward.Action) > 0 {
				forwardAction := make([]types.Action, len(v.ActionForward.Action))
				for i, condition := range v.ActionForward.Action {
					forwardAction[i] = types.Action{
						Type:    condition.Type,
						Content: condition.Content,
					}
				}
				actionForward.Action = forwardAction
			}
		} else {
			actionForward = nil
		}

		resp.Data.Data = append(resp.Data.Data,
			types.SopStageInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &v.ID,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				Status:            &v.Status,
				TaskId:            &v.TaskID,
				Name:              &v.Name,
				ConditionType:     &v.ConditionType,
				ConditionOperator: &v.ConditionOperator,
				ConditionList:     conditionList,
				ActionMessage:     actionMessage,
				ActionLabelAdd:    v.ActionLabelAdd,
				ActionLabelDel:    v.ActionLabelDel,
				ActionForward:     actionForward,
				IndexSort:         &v.IndexSort,
			})
	}

	return resp, nil
}
