// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package provider

import (
	tfTypes "github.com/airbytehq/terraform-provider-airbyte/internal/provider/types"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/models/shared"
	customTypes "github.com/airbytehq/terraform-provider-airbyte/internal/sdk/types"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *SourceShopifyResourceModel) ToSharedSourceShopifyCreateRequest() *shared.SourceShopifyCreateRequest {
	var name string
	name = r.Name.ValueString()

	definitionID := new(string)
	if !r.DefinitionID.IsUnknown() && !r.DefinitionID.IsNull() {
		*definitionID = r.DefinitionID.ValueString()
	} else {
		definitionID = nil
	}
	var workspaceID string
	workspaceID = r.WorkspaceID.ValueString()

	bulkWindowInDays := new(int64)
	if !r.Configuration.BulkWindowInDays.IsUnknown() && !r.Configuration.BulkWindowInDays.IsNull() {
		*bulkWindowInDays = r.Configuration.BulkWindowInDays.ValueInt64()
	} else {
		bulkWindowInDays = nil
	}
	var credentials *shared.ShopifyAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var sourceShopifyOAuth20 *shared.SourceShopifyOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			accessToken := new(string)
			if !r.Configuration.Credentials.OAuth20.AccessToken.IsUnknown() && !r.Configuration.Credentials.OAuth20.AccessToken.IsNull() {
				*accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()
			} else {
				accessToken = nil
			}
			clientID := new(string)
			if !r.Configuration.Credentials.OAuth20.ClientID.IsUnknown() && !r.Configuration.Credentials.OAuth20.ClientID.IsNull() {
				*clientID = r.Configuration.Credentials.OAuth20.ClientID.ValueString()
			} else {
				clientID = nil
			}
			clientSecret := new(string)
			if !r.Configuration.Credentials.OAuth20.ClientSecret.IsUnknown() && !r.Configuration.Credentials.OAuth20.ClientSecret.IsNull() {
				*clientSecret = r.Configuration.Credentials.OAuth20.ClientSecret.ValueString()
			} else {
				clientSecret = nil
			}
			sourceShopifyOAuth20 = &shared.SourceShopifyOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
			}
		}
		if sourceShopifyOAuth20 != nil {
			credentials = &shared.ShopifyAuthorizationMethod{
				SourceShopifyOAuth20: sourceShopifyOAuth20,
			}
		}
		var apiPassword *shared.APIPassword
		if r.Configuration.Credentials.APIPassword != nil {
			var apiPassword1 string
			apiPassword1 = r.Configuration.Credentials.APIPassword.APIPassword.ValueString()

			apiPassword = &shared.APIPassword{
				APIPassword: apiPassword1,
			}
		}
		if apiPassword != nil {
			credentials = &shared.ShopifyAuthorizationMethod{
				APIPassword: apiPassword,
			}
		}
	}
	fetchTransactionsUserID := new(bool)
	if !r.Configuration.FetchTransactionsUserID.IsUnknown() && !r.Configuration.FetchTransactionsUserID.IsNull() {
		*fetchTransactionsUserID = r.Configuration.FetchTransactionsUserID.ValueBool()
	} else {
		fetchTransactionsUserID = nil
	}
	jobCheckpointInterval := new(int64)
	if !r.Configuration.JobCheckpointInterval.IsUnknown() && !r.Configuration.JobCheckpointInterval.IsNull() {
		*jobCheckpointInterval = r.Configuration.JobCheckpointInterval.ValueInt64()
	} else {
		jobCheckpointInterval = nil
	}
	jobProductVariantsIncludePresPrices := new(bool)
	if !r.Configuration.JobProductVariantsIncludePresPrices.IsUnknown() && !r.Configuration.JobProductVariantsIncludePresPrices.IsNull() {
		*jobProductVariantsIncludePresPrices = r.Configuration.JobProductVariantsIncludePresPrices.ValueBool()
	} else {
		jobProductVariantsIncludePresPrices = nil
	}
	jobTerminationThreshold := new(int64)
	if !r.Configuration.JobTerminationThreshold.IsUnknown() && !r.Configuration.JobTerminationThreshold.IsNull() {
		*jobTerminationThreshold = r.Configuration.JobTerminationThreshold.ValueInt64()
	} else {
		jobTerminationThreshold = nil
	}
	var shop string
	shop = r.Configuration.Shop.ValueString()

	startDate := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceShopify{
		BulkWindowInDays:                    bulkWindowInDays,
		Credentials:                         credentials,
		FetchTransactionsUserID:             fetchTransactionsUserID,
		JobCheckpointInterval:               jobCheckpointInterval,
		JobProductVariantsIncludePresPrices: jobProductVariantsIncludePresPrices,
		JobTerminationThreshold:             jobTerminationThreshold,
		Shop:                                shop,
		StartDate:                           startDate,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceShopifyCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceShopifyResourceModel) RefreshFromSharedSourceResponse(resp *shared.SourceResponse) {
	if resp != nil {
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DefinitionID = types.StringValue(resp.DefinitionID)
		r.Name = types.StringValue(resp.Name)
		if resp.ResourceAllocation == nil {
			r.ResourceAllocation = nil
		} else {
			r.ResourceAllocation = &tfTypes.ScopedResourceRequirements{}
			if resp.ResourceAllocation.Default == nil {
				r.ResourceAllocation.Default = nil
			} else {
				r.ResourceAllocation.Default = &tfTypes.ResourceRequirements{}
				r.ResourceAllocation.Default.CPULimit = types.StringPointerValue(resp.ResourceAllocation.Default.CPULimit)
				r.ResourceAllocation.Default.CPURequest = types.StringPointerValue(resp.ResourceAllocation.Default.CPURequest)
				r.ResourceAllocation.Default.EphemeralStorageLimit = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageLimit)
				r.ResourceAllocation.Default.EphemeralStorageRequest = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageRequest)
				r.ResourceAllocation.Default.MemoryLimit = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryLimit)
				r.ResourceAllocation.Default.MemoryRequest = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryRequest)
			}
			r.ResourceAllocation.JobSpecific = []tfTypes.JobTypeResourceLimit{}
			if len(r.ResourceAllocation.JobSpecific) > len(resp.ResourceAllocation.JobSpecific) {
				r.ResourceAllocation.JobSpecific = r.ResourceAllocation.JobSpecific[:len(resp.ResourceAllocation.JobSpecific)]
			}
			for jobSpecificCount, jobSpecificItem := range resp.ResourceAllocation.JobSpecific {
				var jobSpecific1 tfTypes.JobTypeResourceLimit
				jobSpecific1.JobType = types.StringValue(string(jobSpecificItem.JobType))
				jobSpecific1.ResourceRequirements.CPULimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPULimit)
				jobSpecific1.ResourceRequirements.CPURequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPURequest)
				jobSpecific1.ResourceRequirements.EphemeralStorageLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageLimit)
				jobSpecific1.ResourceRequirements.EphemeralStorageRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageRequest)
				jobSpecific1.ResourceRequirements.MemoryLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryLimit)
				jobSpecific1.ResourceRequirements.MemoryRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryRequest)
				if jobSpecificCount+1 > len(r.ResourceAllocation.JobSpecific) {
					r.ResourceAllocation.JobSpecific = append(r.ResourceAllocation.JobSpecific, jobSpecific1)
				} else {
					r.ResourceAllocation.JobSpecific[jobSpecificCount].JobType = jobSpecific1.JobType
					r.ResourceAllocation.JobSpecific[jobSpecificCount].ResourceRequirements = jobSpecific1.ResourceRequirements
				}
			}
		}
		r.SourceID = types.StringValue(resp.SourceID)
		r.SourceType = types.StringValue(resp.SourceType)
		r.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *SourceShopifyResourceModel) ToSharedSourceShopifyPutRequest() *shared.SourceShopifyPutRequest {
	var name string
	name = r.Name.ValueString()

	var workspaceID string
	workspaceID = r.WorkspaceID.ValueString()

	bulkWindowInDays := new(int64)
	if !r.Configuration.BulkWindowInDays.IsUnknown() && !r.Configuration.BulkWindowInDays.IsNull() {
		*bulkWindowInDays = r.Configuration.BulkWindowInDays.ValueInt64()
	} else {
		bulkWindowInDays = nil
	}
	var credentials *shared.SourceShopifyUpdateShopifyAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var sourceShopifyUpdateOAuth20 *shared.SourceShopifyUpdateOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			accessToken := new(string)
			if !r.Configuration.Credentials.OAuth20.AccessToken.IsUnknown() && !r.Configuration.Credentials.OAuth20.AccessToken.IsNull() {
				*accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()
			} else {
				accessToken = nil
			}
			clientID := new(string)
			if !r.Configuration.Credentials.OAuth20.ClientID.IsUnknown() && !r.Configuration.Credentials.OAuth20.ClientID.IsNull() {
				*clientID = r.Configuration.Credentials.OAuth20.ClientID.ValueString()
			} else {
				clientID = nil
			}
			clientSecret := new(string)
			if !r.Configuration.Credentials.OAuth20.ClientSecret.IsUnknown() && !r.Configuration.Credentials.OAuth20.ClientSecret.IsNull() {
				*clientSecret = r.Configuration.Credentials.OAuth20.ClientSecret.ValueString()
			} else {
				clientSecret = nil
			}
			sourceShopifyUpdateOAuth20 = &shared.SourceShopifyUpdateOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
			}
		}
		if sourceShopifyUpdateOAuth20 != nil {
			credentials = &shared.SourceShopifyUpdateShopifyAuthorizationMethod{
				SourceShopifyUpdateOAuth20: sourceShopifyUpdateOAuth20,
			}
		}
		var sourceShopifyUpdateAPIPassword *shared.SourceShopifyUpdateAPIPassword
		if r.Configuration.Credentials.APIPassword != nil {
			var apiPassword string
			apiPassword = r.Configuration.Credentials.APIPassword.APIPassword.ValueString()

			sourceShopifyUpdateAPIPassword = &shared.SourceShopifyUpdateAPIPassword{
				APIPassword: apiPassword,
			}
		}
		if sourceShopifyUpdateAPIPassword != nil {
			credentials = &shared.SourceShopifyUpdateShopifyAuthorizationMethod{
				SourceShopifyUpdateAPIPassword: sourceShopifyUpdateAPIPassword,
			}
		}
	}
	fetchTransactionsUserID := new(bool)
	if !r.Configuration.FetchTransactionsUserID.IsUnknown() && !r.Configuration.FetchTransactionsUserID.IsNull() {
		*fetchTransactionsUserID = r.Configuration.FetchTransactionsUserID.ValueBool()
	} else {
		fetchTransactionsUserID = nil
	}
	jobCheckpointInterval := new(int64)
	if !r.Configuration.JobCheckpointInterval.IsUnknown() && !r.Configuration.JobCheckpointInterval.IsNull() {
		*jobCheckpointInterval = r.Configuration.JobCheckpointInterval.ValueInt64()
	} else {
		jobCheckpointInterval = nil
	}
	jobProductVariantsIncludePresPrices := new(bool)
	if !r.Configuration.JobProductVariantsIncludePresPrices.IsUnknown() && !r.Configuration.JobProductVariantsIncludePresPrices.IsNull() {
		*jobProductVariantsIncludePresPrices = r.Configuration.JobProductVariantsIncludePresPrices.ValueBool()
	} else {
		jobProductVariantsIncludePresPrices = nil
	}
	jobTerminationThreshold := new(int64)
	if !r.Configuration.JobTerminationThreshold.IsUnknown() && !r.Configuration.JobTerminationThreshold.IsNull() {
		*jobTerminationThreshold = r.Configuration.JobTerminationThreshold.ValueInt64()
	} else {
		jobTerminationThreshold = nil
	}
	var shop string
	shop = r.Configuration.Shop.ValueString()

	startDate := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceShopifyUpdate{
		BulkWindowInDays:                    bulkWindowInDays,
		Credentials:                         credentials,
		FetchTransactionsUserID:             fetchTransactionsUserID,
		JobCheckpointInterval:               jobCheckpointInterval,
		JobProductVariantsIncludePresPrices: jobProductVariantsIncludePresPrices,
		JobTerminationThreshold:             jobTerminationThreshold,
		Shop:                                shop,
		StartDate:                           startDate,
	}
	out := shared.SourceShopifyPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
