package crm

import (
	"context"

	"apps/backend/internal/logic/crmmap"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *MergeContactLogic) MergeContact(req *types.MergeContactReq) (*types.ContactPublic, error) {
	uid, err := ownerUID(l.ctx)
	if err != nil {
		return nil, err
	}
	c, err := l.svcCtx.Crm.Merge(l.ctx, uid, req.Id, req.SourceContactId)
	if err != nil {
		return nil, err
	}
	return crmmap.Contact(c), nil
}
