package sop_stage

import (
	"context"
	"fmt"

	"wechat-api/ent/predicate"
	"wechat-api/ent/sopstage"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *GetSopStageListLogic) GetSopStageList(req *types.SopStageListReq) (*types.SopStageListResp, error) {
	var predicates []predicate.SopStage
	if req.TaskId != nil {
		predicates = append(predicates, sopstage.TaskID(*req.TaskId))
	} else {
		return nil, dberrorhandler.DefaultEntError(l.Logger, fmt.Errorf("TaskID 不能为空"), req)
	}
	data, err := l.svcCtx.DB.SopStage.Query().Where(predicates...).All(l.ctx)

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

	resp := &types.SopStageListResp{}
	resp.Msg = errormsg.Success

	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
}
