package label_relationship

import (
	"context"

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

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

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

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

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

func (l *CreateLabelRelationshipLogic) CreateLabelRelationship(req *types.LabelRelationshipInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	_, err := l.svcCtx.DB.LabelRelationship.Create().
		SetNotNilStatus(req.Status).
		SetNotNilLabelID(req.LabelId).
		SetNotNilContactID(req.ContactId).
		SetOrganizationID(organizationId).
		Save(l.ctx)

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

	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
}
