package label_tagging

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"wechat-api/ent/labeltagging"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *GetLabelTaggingByIdLogic) GetLabelTaggingById(req *types.IDReq) (resp *types.LabelTaggingInfoResp, err error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	data, err := l.svcCtx.DB.LabelTagging.Query().Where(labeltagging.ID(req.Id), labeltagging.OrganizationID(organizationId)).Only(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.LabelTaggingInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.LabelTaggingInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:         &data.Status,
			OrganizationId: &data.OrganizationID,
			Type:           &data.Type,
			Conditions:     data.Conditions,
			ActionLabelAdd: data.ActionLabelAdd,
			ActionLabelDel: data.ActionLabelDel,
		},
	}, nil
}
