package rules

import (
	"code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts"
	"code.tnxs.net/taxnexus/lib/api/crm/crm_models"
	"code.tnxs.net/taxnexus/lib/app"
	httptransport "github.com/go-openapi/runtime/client"
	"go.temporal.io/sdk/workflow"
)

// StoreAccountActivityID is an activity identifier
const StoreAccountActivityID = "STORE_ACCOUNT_ACTIVITY"

// StoreAccountActivity posts a new account object to datastore
func StoreAccountActivity(ctx workflow.Context, w app.AccountActivityWrapper) error { //nolint:gocritic // what we want
	obj := &crm_models.Account{
		AccountSource:            w.AccountSource,
		AdministrativeLevel:      w.AdministrativeLevel,
		BillingAddress:           w.BillingAddress.MarshalToCrm(),
		BillingContactID:         w.BillingContactID,
		BillingPreference:        w.BillingPreference,
		BusinessAddress:          w.BusinessAddress.MarshalToCrm(),
		CannabisCustomer:         w.CannabisCustomer,
		CompanyID:                w.CompanyID,
		CoordinateID:             w.CoordinateID,
		CustomerID:               w.CustomerID,
		CustomerPriority:         w.CustomerPriority,
		DBA:                      w.DBA,
		DefaultAddress:           w.DefaultAddress.MarshalToCrm(),
		DefaultBackendID:         w.DefaultBackendID,
		DefaultDeliveryContactID: w.DefaultDeliveryContactID,
		DefaultEndUserID:         w.DefaultEndUserID,
		Description:              w.Description,
		EIN:                      w.EIN,
		Email:                    w.Email,
		EnrollmentStatus:         w.EnrollmentStatus,
		Fax:                      w.Fax,
		ISPCustomer:              w.ISPCustomer,
		MSPCustomer:              w.MSPCustomer,
		Name:                     w.Name,
		OrderContactID:           w.OrderContactID,
		OrderEmail:               w.OrderEmail,
		OwnerID:                  w.OwnerID,
		ParentID:                 w.ParentID,
		Phone:                    w.Phone,
		PlaceID:                  w.PlaceID,
		PreparerID:               w.PreparerID,
		RatingEngineID:           w.RatingEngineID,
		Ref:                      w.Ref,
		ShippingAddress:          w.ShippingAddress.MarshalToCrm(),
		Site:                     w.Site,
		TelecomCustomer:          w.TelecomCustomer,
		TenantID:                 w.TenantID,
		Type:                     w.Type,
		Website:                  w.Website,
	}

	postAccountParams := accounts.NewPostAccountsParamsWithTimeout(postTimeout)
	postAccountParams.AccountRequest = &crm_models.AccountRequest{
		Data: []*crm_models.Account{obj},
	}
	_, err := crmClient.Accounts.PostAccounts(
		postAccountParams,
		httptransport.APIKeyAuth(
			"X-API-Key",
			"header",
			w.APIKey,
		))
	if err != nil {
		return err
	}
	sugar.Info("rules.StoreAccountActivity: 👍 📤")
	return nil
}
