package sop_stage

import (
	"context"
	"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 GetSopStageByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *GetSopStageByIdLogic) GetSopStageById(req *types.IDReq) (*types.SopStageInfoResp, error) {
	data, err := l.svcCtx.DB.SopStage.Query().
		Where(sopstage.ID(req.Id)).
		WithStageNodes().
		Only(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

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

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

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

	var nodeList []types.SopNodeInfo
	if data.Edges.StageNodes != nil {
		nodeList = make([]types.SopNodeInfo, 0)
		for _, sn := range data.Edges.StageNodes {
			nodeList = append(nodeList, types.SopNodeInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id: &sn.ID,
				},
				Status:        &sn.Status,
				ParentId:      &sn.ParentID,
				Name:          &sn.Name,
				ConditionType: &sn.ConditionType,
			})
		}
	}

	return &types.SopStageInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.SopStageInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:            &data.Status,
			TaskId:            &data.TaskID,
			Name:              &data.Name,
			ConditionType:     &data.ConditionType,
			ConditionOperator: &data.ConditionOperator,
			ConditionList:     conditionList,
			ActionMessage:     actionMessage,
			ActionLabelAdd:    data.ActionLabelAdd,
			ActionLabelDel:    data.ActionLabelDel,
			ActionForward:     actionForward,
			IndexSort:         &data.IndexSort,
			NodeList:          nodeList,
		},
	}, nil
}
