package rules

import (
	"go.temporal.io/sdk/temporal"
	temporal_workflow "go.temporal.io/sdk/workflow"
)

// NewLeadWorkflowID is the text identifier for new-lead-workflow
const NewLeadWorkflowID = "new-lead-workflow"

// NewLeadWorkflowWrapper wraps a Lead with auth and saga info
type NewLeadWorkflowWrapper struct {
	APIKey   string
	SagaID   string
	SagaType string
	//
	Company     string
	Description string
	Email       string
	FirstName   string
	LastName    string
	UTMSource   string
}

// NewLeadWorkflow is a Temporal workflow
func NewLeadWorkflow(ctx temporal_workflow.Context, w NewLeadWorkflowWrapper) error { //nolint:gocritic // don't care
	sugar.Info("rules.NewLeadWorkflow: 📥")
	sugar.Debugf("rules.NewLeadWorkflow: ctx: %v", ctx)
	sugar.Debugf("rules.NewLeadWorkflow: w: %v", w)
	activityDetails := NewLeadActivityWrapper(w)
	retryPolicy := &temporal.RetryPolicy{
		InitialInterval:    initialInterval,
		BackoffCoefficient: backoffCoefficient,
		MaximumInterval:    maximumInterval,
		MaximumAttempts:    maximumAttempts,
	}
	options := temporal_workflow.ActivityOptions{
		TaskQueue:              TaxnexusRulesTaskQueueID,
		ScheduleToCloseTimeout: scheduleToCloseTimeout,
		ScheduleToStartTimeout: scheduleToStartTimeout,
		StartToCloseTimeout:    startToCloseTimeout,
		HeartbeatTimeout:       heartbeatTimeout,
		WaitForCancellation:    waitForCancelation,
		ActivityID:             NewLeadWorkflowID,
		RetryPolicy:            retryPolicy,
	}
	ctx = temporal_workflow.WithActivityOptions(ctx, options)
	sugar.Debugf("rules.NewLeadWorkflow: 📏 do Store", ctx)
	err := temporal_workflow.ExecuteActivity(
		ctx,
		StoreLeadActivity,
		activityDetails,
	).Get(ctx, nil)
	if err != nil {
		return err
	}
	sugar.Debugf("rules.NewLeadWorkflow: 📏 do Notify", ctx)
	err = temporal_workflow.ExecuteActivity(
		ctx,
		NotifyLeadActivity,
		activityDetails,
	).Get(ctx, nil)
	if err != nil {
		return err
	}
	sugar.Info("rules.NewLeadWorkflow: 👍 📤")
	return nil
}
