package crm

import (
	"context"

	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *CreateContactConversionLogic) CreateContactConversion(req *types.CreateCrmConversionReq) (*types.CrmConversionData, error) {
	uid, err := ownerUID(l.ctx)
	if err != nil {
		return nil, err
	}
	c, outcomeID, err := l.svcCtx.Crm.ReportConversion(l.ctx, uid, req.Id, req.Amount, req.Currency, req.Note)
	if err != nil {
		return nil, err
	}
	return &types.CrmConversionData{
		ContactId: c.ID, OutcomeId: outcomeID, Stage: c.Stage,
		Amount: req.Amount, Currency: req.Currency, Note: req.Note, UpdatedAt: c.UpdatedAt,
	}, nil
}
