package label

import (
	"context"
	"wechat-api/ent/label"
	"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 GetLabelByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *GetLabelByIdLogic) GetLabelById(req *types.IDReq) (*types.LabelInfoResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	data, err := l.svcCtx.DB.Label.Query().
		Where(
			label.IDEQ(req.Id),                   // Filter by ID
			label.OrganizationID(organizationId), // Additional filter by organizationId
		).
		Only(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.LabelInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.LabelInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:     &data.Status,
			Type:       &data.Type,
			Name:       &data.Name,
			From:       &data.From,
			Mode:       &data.Mode,
			Conditions: &data.Conditions,
		},
	}, nil
}
