package label_relationship

import (
	"context"
	"wechat-api/ent/labelrelationship"

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

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

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

	return &types.LabelRelationshipInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.LabelRelationshipInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:    &data.Status,
			LabelId:   &data.LabelID,
			ContactId: &data.ContactID,
		},
	}, nil
}
