package rules

import (
	"time"

	"code.tnxs.net/taxnexus/lib/app"
	"go.temporal.io/sdk/workflow"
)

// NewDeveloperWorkflowWrapper wraps a contact, account, payment method with auth and saga info
type NewDeveloperWorkflowWrapper struct {
	AccountName       string
	BillingStreet     string
	BillingCity       string
	BillingState      string
	BillingPostalCode string
	Company           string
	Description       string
	Email             string
	FirstName         string
	LastName          string
	MobilePhone       string
	Name              string
	Phone             string
	Title             string
	Website           string
	APIKey            string
	SagaID            string
	SagaType          string
}

// NewDeveloperWorkflow is a Temporal workflow
func NewDeveloperWorkflow(ctx workflow.Context, w NewDeveloperWorkflowWrapper) error { //nolint:gocritic // don't care
	ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
		StartToCloseTimeout: time.Minute,
	})
	err := workflow.ExecuteActivity(ctx,
		StoreContactActivity,
		app.ContactActivityWrapper{
			Description: w.Description,
			Email:       w.Email,
			FirstName:   w.FirstName,
			LastName:    w.LastName,
			MobilePhone: w.MobilePhone,
			Name:        w.Name,
			Phone:       w.Phone,
			Title:       w.Title,
			APIKey:      w.APIKey,
			SagaID:      w.SagaID,
			SagaType:    w.SagaType,
		}).Get(ctx, nil)
	if err != nil {
		return err
	}
	err = workflow.ExecuteActivity(ctx,
		StoreAccountActivity,
		&app.AccountActivityWrapper{
			APIKey:   w.APIKey,
			SagaID:   w.SagaID,
			SagaType: w.SagaType,
			//
			Description: w.Description,
			Email:       w.Email,
			Name:        w.AccountName,
		}).Get(ctx, nil)
	if err != nil {
		return err
	}
	err = workflow.ExecuteActivity(ctx,
		StorePaymentMethodActivity,
		&app.PaymentMethodActivityWrapper{
			APIKey:   w.APIKey,
			SagaID:   w.SagaID,
			SagaType: w.SagaType,
			//
			AccountID:        "todo fix this",
			AchAccountType:   "",
			AchBankAccount:   "",
			AchRouting:       "",
			Active:           false,
			Autopay:          false,
			BankName:         "",
			BillingContactID: "",
			CCnumber:         "",
			CCtype:           "",
			ContractID:       "",
			Default:          false,
			ExpirationMonth:  "",
			ExpirationYear:   "",
			Gateway:          "",
			GatewayKey:       "",
			Nickname:         "",
			RecordType:       "",
			Ref:              "",
			TenantID:         "",
		}).Get(ctx, nil)
	if err != nil {
		return err
	}
	err = workflow.ExecuteActivity(ctx,
		NotifyContactActivity,
		&app.ContactActivityWrapper{
			APIKey:   w.APIKey,
			SagaID:   w.SagaID,
			SagaType: w.SagaType,
			//
			Description: w.Description,
			Email:       w.Email,
			FirstName:   w.FirstName,
			LastName:    w.LastName,
			MobilePhone: w.MobilePhone,
			Name:        w.Name,
			Phone:       w.Phone,
			Title:       w.Title,
		}).Get(ctx, nil)
	if err != nil {
		return err
	}
	err = workflow.ExecuteActivity(ctx, NotifyLeadActivity, w).Get(ctx, nil)
	if err != nil {
		return err
	}
	return nil
}
