// 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"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *DestinationBigqueryResourceModel) ToSharedDestinationBigqueryCreateRequest() *shared.DestinationBigqueryCreateRequest {
	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()

	bigQueryClientBufferSizeMb := new(int64)
	if !r.Configuration.BigQueryClientBufferSizeMb.IsUnknown() && !r.Configuration.BigQueryClientBufferSizeMb.IsNull() {
		*bigQueryClientBufferSizeMb = r.Configuration.BigQueryClientBufferSizeMb.ValueInt64()
	} else {
		bigQueryClientBufferSizeMb = nil
	}
	credentialsJSON := new(string)
	if !r.Configuration.CredentialsJSON.IsUnknown() && !r.Configuration.CredentialsJSON.IsNull() {
		*credentialsJSON = r.Configuration.CredentialsJSON.ValueString()
	} else {
		credentialsJSON = nil
	}
	var datasetID string
	datasetID = r.Configuration.DatasetID.ValueString()

	datasetLocation := shared.DatasetLocation(r.Configuration.DatasetLocation.ValueString())
	disableTypeDedupe := new(bool)
	if !r.Configuration.DisableTypeDedupe.IsUnknown() && !r.Configuration.DisableTypeDedupe.IsNull() {
		*disableTypeDedupe = r.Configuration.DisableTypeDedupe.ValueBool()
	} else {
		disableTypeDedupe = nil
	}
	var loadingMethod *shared.LoadingMethod
	if r.Configuration.LoadingMethod != nil {
		var batchedStandardInserts *shared.BatchedStandardInserts
		if r.Configuration.LoadingMethod.BatchedStandardInserts != nil {
			batchedStandardInserts = &shared.BatchedStandardInserts{}
		}
		if batchedStandardInserts != nil {
			loadingMethod = &shared.LoadingMethod{
				BatchedStandardInserts: batchedStandardInserts,
			}
		}
		var gcsStaging *shared.GCSStaging
		if r.Configuration.LoadingMethod.GCSStaging != nil {
			var credential shared.Credential
			var destinationBigqueryHMACKey *shared.DestinationBigqueryHMACKey
			if r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey != nil {
				var hmacKeyAccessID string
				hmacKeyAccessID = r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey.HmacKeyAccessID.ValueString()

				var hmacKeySecret string
				hmacKeySecret = r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey.HmacKeySecret.ValueString()

				destinationBigqueryHMACKey = &shared.DestinationBigqueryHMACKey{
					HmacKeyAccessID: hmacKeyAccessID,
					HmacKeySecret:   hmacKeySecret,
				}
			}
			if destinationBigqueryHMACKey != nil {
				credential = shared.Credential{
					DestinationBigqueryHMACKey: destinationBigqueryHMACKey,
				}
			}
			var gcsBucketName string
			gcsBucketName = r.Configuration.LoadingMethod.GCSStaging.GcsBucketName.ValueString()

			var gcsBucketPath string
			gcsBucketPath = r.Configuration.LoadingMethod.GCSStaging.GcsBucketPath.ValueString()

			keepFilesInGcsBucket := new(shared.GCSTmpFilesAfterwardProcessing)
			if !r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.IsUnknown() && !r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.IsNull() {
				*keepFilesInGcsBucket = shared.GCSTmpFilesAfterwardProcessing(r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.ValueString())
			} else {
				keepFilesInGcsBucket = nil
			}
			gcsStaging = &shared.GCSStaging{
				Credential:           credential,
				GcsBucketName:        gcsBucketName,
				GcsBucketPath:        gcsBucketPath,
				KeepFilesInGcsBucket: keepFilesInGcsBucket,
			}
		}
		if gcsStaging != nil {
			loadingMethod = &shared.LoadingMethod{
				GCSStaging: gcsStaging,
			}
		}
	}
	var projectID string
	projectID = r.Configuration.ProjectID.ValueString()

	rawDataDataset := new(string)
	if !r.Configuration.RawDataDataset.IsUnknown() && !r.Configuration.RawDataDataset.IsNull() {
		*rawDataDataset = r.Configuration.RawDataDataset.ValueString()
	} else {
		rawDataDataset = nil
	}
	transformationPriority := new(shared.TransformationQueryRunType)
	if !r.Configuration.TransformationPriority.IsUnknown() && !r.Configuration.TransformationPriority.IsNull() {
		*transformationPriority = shared.TransformationQueryRunType(r.Configuration.TransformationPriority.ValueString())
	} else {
		transformationPriority = nil
	}
	configuration := shared.DestinationBigquery{
		BigQueryClientBufferSizeMb: bigQueryClientBufferSizeMb,
		CredentialsJSON:            credentialsJSON,
		DatasetID:                  datasetID,
		DatasetLocation:            datasetLocation,
		DisableTypeDedupe:          disableTypeDedupe,
		LoadingMethod:              loadingMethod,
		ProjectID:                  projectID,
		RawDataDataset:             rawDataDataset,
		TransformationPriority:     transformationPriority,
	}
	out := shared.DestinationBigqueryCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationBigqueryResourceModel) RefreshFromSharedDestinationResponse(resp *shared.DestinationResponse) {
	if resp != nil {
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DefinitionID = types.StringValue(resp.DefinitionID)
		r.DestinationID = types.StringValue(resp.DestinationID)
		r.DestinationType = types.StringValue(resp.DestinationType)
		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.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *DestinationBigqueryResourceModel) ToSharedDestinationBigqueryPutRequest() *shared.DestinationBigqueryPutRequest {
	var name string
	name = r.Name.ValueString()

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

	bigQueryClientBufferSizeMb := new(int64)
	if !r.Configuration.BigQueryClientBufferSizeMb.IsUnknown() && !r.Configuration.BigQueryClientBufferSizeMb.IsNull() {
		*bigQueryClientBufferSizeMb = r.Configuration.BigQueryClientBufferSizeMb.ValueInt64()
	} else {
		bigQueryClientBufferSizeMb = nil
	}
	credentialsJSON := new(string)
	if !r.Configuration.CredentialsJSON.IsUnknown() && !r.Configuration.CredentialsJSON.IsNull() {
		*credentialsJSON = r.Configuration.CredentialsJSON.ValueString()
	} else {
		credentialsJSON = nil
	}
	var datasetID string
	datasetID = r.Configuration.DatasetID.ValueString()

	datasetLocation := shared.DestinationBigqueryUpdateDatasetLocation(r.Configuration.DatasetLocation.ValueString())
	disableTypeDedupe := new(bool)
	if !r.Configuration.DisableTypeDedupe.IsUnknown() && !r.Configuration.DisableTypeDedupe.IsNull() {
		*disableTypeDedupe = r.Configuration.DisableTypeDedupe.ValueBool()
	} else {
		disableTypeDedupe = nil
	}
	var loadingMethod *shared.DestinationBigqueryUpdateLoadingMethod
	if r.Configuration.LoadingMethod != nil {
		var destinationBigqueryUpdateBatchedStandardInserts *shared.DestinationBigqueryUpdateBatchedStandardInserts
		if r.Configuration.LoadingMethod.BatchedStandardInserts != nil {
			destinationBigqueryUpdateBatchedStandardInserts = &shared.DestinationBigqueryUpdateBatchedStandardInserts{}
		}
		if destinationBigqueryUpdateBatchedStandardInserts != nil {
			loadingMethod = &shared.DestinationBigqueryUpdateLoadingMethod{
				DestinationBigqueryUpdateBatchedStandardInserts: destinationBigqueryUpdateBatchedStandardInserts,
			}
		}
		var destinationBigqueryUpdateGCSStaging *shared.DestinationBigqueryUpdateGCSStaging
		if r.Configuration.LoadingMethod.GCSStaging != nil {
			var credential shared.DestinationBigqueryUpdateCredential
			var destinationBigqueryUpdateHMACKey *shared.DestinationBigqueryUpdateHMACKey
			if r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey != nil {
				var hmacKeyAccessID string
				hmacKeyAccessID = r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey.HmacKeyAccessID.ValueString()

				var hmacKeySecret string
				hmacKeySecret = r.Configuration.LoadingMethod.GCSStaging.Credential.HMACKey.HmacKeySecret.ValueString()

				destinationBigqueryUpdateHMACKey = &shared.DestinationBigqueryUpdateHMACKey{
					HmacKeyAccessID: hmacKeyAccessID,
					HmacKeySecret:   hmacKeySecret,
				}
			}
			if destinationBigqueryUpdateHMACKey != nil {
				credential = shared.DestinationBigqueryUpdateCredential{
					DestinationBigqueryUpdateHMACKey: destinationBigqueryUpdateHMACKey,
				}
			}
			var gcsBucketName string
			gcsBucketName = r.Configuration.LoadingMethod.GCSStaging.GcsBucketName.ValueString()

			var gcsBucketPath string
			gcsBucketPath = r.Configuration.LoadingMethod.GCSStaging.GcsBucketPath.ValueString()

			keepFilesInGcsBucket := new(shared.DestinationBigqueryUpdateGCSTmpFilesAfterwardProcessing)
			if !r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.IsUnknown() && !r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.IsNull() {
				*keepFilesInGcsBucket = shared.DestinationBigqueryUpdateGCSTmpFilesAfterwardProcessing(r.Configuration.LoadingMethod.GCSStaging.KeepFilesInGcsBucket.ValueString())
			} else {
				keepFilesInGcsBucket = nil
			}
			destinationBigqueryUpdateGCSStaging = &shared.DestinationBigqueryUpdateGCSStaging{
				Credential:           credential,
				GcsBucketName:        gcsBucketName,
				GcsBucketPath:        gcsBucketPath,
				KeepFilesInGcsBucket: keepFilesInGcsBucket,
			}
		}
		if destinationBigqueryUpdateGCSStaging != nil {
			loadingMethod = &shared.DestinationBigqueryUpdateLoadingMethod{
				DestinationBigqueryUpdateGCSStaging: destinationBigqueryUpdateGCSStaging,
			}
		}
	}
	var projectID string
	projectID = r.Configuration.ProjectID.ValueString()

	rawDataDataset := new(string)
	if !r.Configuration.RawDataDataset.IsUnknown() && !r.Configuration.RawDataDataset.IsNull() {
		*rawDataDataset = r.Configuration.RawDataDataset.ValueString()
	} else {
		rawDataDataset = nil
	}
	transformationPriority := new(shared.DestinationBigqueryUpdateTransformationQueryRunType)
	if !r.Configuration.TransformationPriority.IsUnknown() && !r.Configuration.TransformationPriority.IsNull() {
		*transformationPriority = shared.DestinationBigqueryUpdateTransformationQueryRunType(r.Configuration.TransformationPriority.ValueString())
	} else {
		transformationPriority = nil
	}
	configuration := shared.DestinationBigqueryUpdate{
		BigQueryClientBufferSizeMb: bigQueryClientBufferSizeMb,
		CredentialsJSON:            credentialsJSON,
		DatasetID:                  datasetID,
		DatasetLocation:            datasetLocation,
		DisableTypeDedupe:          disableTypeDedupe,
		LoadingMethod:              loadingMethod,
		ProjectID:                  projectID,
		RawDataDataset:             rawDataDataset,
		TransformationPriority:     transformationPriority,
	}
	out := shared.DestinationBigqueryPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
