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

package provider

import (
	"encoding/json"
	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 *SourceMongodbV2ResourceModel) ToSharedSourceMongodbV2CreateRequest() *shared.SourceMongodbV2CreateRequest {
	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()

	var databaseConfig shared.ClusterType
	var mongoDBAtlasReplicaSet *shared.MongoDBAtlasReplicaSet
	if r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet != nil {
		authSource := new(string)
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.IsNull() {
			*authSource = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.ValueString()
		} else {
			authSource = nil
		}
		var connectionString string
		connectionString = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.ConnectionString.ValueString()

		var databases []string = []string{}
		for _, databasesItem := range r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Databases {
			databases = append(databases, databasesItem.ValueString())
		}
		var password string
		password = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Password.ValueString()

		schemaEnforced := new(bool)
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.IsNull() {
			*schemaEnforced = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.ValueBool()
		} else {
			schemaEnforced = nil
		}
		var username string
		username = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Username.ValueString()

		var additionalProperties interface{}
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.ValueString()), &additionalProperties)
		}
		mongoDBAtlasReplicaSet = &shared.MongoDBAtlasReplicaSet{
			AuthSource:           authSource,
			ConnectionString:     connectionString,
			Databases:            databases,
			Password:             password,
			SchemaEnforced:       schemaEnforced,
			Username:             username,
			AdditionalProperties: additionalProperties,
		}
	}
	if mongoDBAtlasReplicaSet != nil {
		databaseConfig = shared.ClusterType{
			MongoDBAtlasReplicaSet: mongoDBAtlasReplicaSet,
		}
	}
	var selfManagedReplicaSet *shared.SelfManagedReplicaSet
	if r.Configuration.DatabaseConfig.SelfManagedReplicaSet != nil {
		authSource1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.IsNull() {
			*authSource1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.ValueString()
		} else {
			authSource1 = nil
		}
		var connectionString1 string
		connectionString1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.ConnectionString.ValueString()

		var databases1 []string = []string{}
		for _, databasesItem1 := range r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Databases {
			databases1 = append(databases1, databasesItem1.ValueString())
		}
		password1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.IsNull() {
			*password1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.ValueString()
		} else {
			password1 = nil
		}
		schemaEnforced1 := new(bool)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.IsNull() {
			*schemaEnforced1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.ValueBool()
		} else {
			schemaEnforced1 = nil
		}
		username1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.IsNull() {
			*username1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.ValueString()
		} else {
			username1 = nil
		}
		var additionalProperties1 interface{}
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.ValueString()), &additionalProperties1)
		}
		selfManagedReplicaSet = &shared.SelfManagedReplicaSet{
			AuthSource:           authSource1,
			ConnectionString:     connectionString1,
			Databases:            databases1,
			Password:             password1,
			SchemaEnforced:       schemaEnforced1,
			Username:             username1,
			AdditionalProperties: additionalProperties1,
		}
	}
	if selfManagedReplicaSet != nil {
		databaseConfig = shared.ClusterType{
			SelfManagedReplicaSet: selfManagedReplicaSet,
		}
	}
	discoverSampleSize := new(int64)
	if !r.Configuration.DiscoverSampleSize.IsUnknown() && !r.Configuration.DiscoverSampleSize.IsNull() {
		*discoverSampleSize = r.Configuration.DiscoverSampleSize.ValueInt64()
	} else {
		discoverSampleSize = nil
	}
	discoverTimeoutSeconds := new(int64)
	if !r.Configuration.DiscoverTimeoutSeconds.IsUnknown() && !r.Configuration.DiscoverTimeoutSeconds.IsNull() {
		*discoverTimeoutSeconds = r.Configuration.DiscoverTimeoutSeconds.ValueInt64()
	} else {
		discoverTimeoutSeconds = nil
	}
	initialLoadTimeoutHours := new(int64)
	if !r.Configuration.InitialLoadTimeoutHours.IsUnknown() && !r.Configuration.InitialLoadTimeoutHours.IsNull() {
		*initialLoadTimeoutHours = r.Configuration.InitialLoadTimeoutHours.ValueInt64()
	} else {
		initialLoadTimeoutHours = nil
	}
	initialWaitingSeconds := new(int64)
	if !r.Configuration.InitialWaitingSeconds.IsUnknown() && !r.Configuration.InitialWaitingSeconds.IsNull() {
		*initialWaitingSeconds = r.Configuration.InitialWaitingSeconds.ValueInt64()
	} else {
		initialWaitingSeconds = nil
	}
	invalidCdcCursorPositionBehavior := new(shared.InvalidCDCPositionBehaviorAdvanced)
	if !r.Configuration.InvalidCdcCursorPositionBehavior.IsUnknown() && !r.Configuration.InvalidCdcCursorPositionBehavior.IsNull() {
		*invalidCdcCursorPositionBehavior = shared.InvalidCDCPositionBehaviorAdvanced(r.Configuration.InvalidCdcCursorPositionBehavior.ValueString())
	} else {
		invalidCdcCursorPositionBehavior = nil
	}
	queueSize := new(int64)
	if !r.Configuration.QueueSize.IsUnknown() && !r.Configuration.QueueSize.IsNull() {
		*queueSize = r.Configuration.QueueSize.ValueInt64()
	} else {
		queueSize = nil
	}
	updateCaptureMode := new(shared.CaptureModeAdvanced)
	if !r.Configuration.UpdateCaptureMode.IsUnknown() && !r.Configuration.UpdateCaptureMode.IsNull() {
		*updateCaptureMode = shared.CaptureModeAdvanced(r.Configuration.UpdateCaptureMode.ValueString())
	} else {
		updateCaptureMode = nil
	}
	configuration := shared.SourceMongodbV2{
		DatabaseConfig:                   databaseConfig,
		DiscoverSampleSize:               discoverSampleSize,
		DiscoverTimeoutSeconds:           discoverTimeoutSeconds,
		InitialLoadTimeoutHours:          initialLoadTimeoutHours,
		InitialWaitingSeconds:            initialWaitingSeconds,
		InvalidCdcCursorPositionBehavior: invalidCdcCursorPositionBehavior,
		QueueSize:                        queueSize,
		UpdateCaptureMode:                updateCaptureMode,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceMongodbV2CreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceMongodbV2ResourceModel) 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 *SourceMongodbV2ResourceModel) ToSharedSourceMongodbV2PutRequest() *shared.SourceMongodbV2PutRequest {
	var name string
	name = r.Name.ValueString()

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

	var databaseConfig shared.SourceMongodbV2UpdateClusterType
	var sourceMongodbV2UpdateMongoDBAtlasReplicaSet *shared.SourceMongodbV2UpdateMongoDBAtlasReplicaSet
	if r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet != nil {
		authSource := new(string)
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.IsNull() {
			*authSource = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AuthSource.ValueString()
		} else {
			authSource = nil
		}
		var connectionString string
		connectionString = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.ConnectionString.ValueString()

		var databases []string = []string{}
		for _, databasesItem := range r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Databases {
			databases = append(databases, databasesItem.ValueString())
		}
		var password string
		password = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Password.ValueString()

		schemaEnforced := new(bool)
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.IsNull() {
			*schemaEnforced = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.SchemaEnforced.ValueBool()
		} else {
			schemaEnforced = nil
		}
		var username string
		username = r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.Username.ValueString()

		var additionalProperties interface{}
		if !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.IsUnknown() && !r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.DatabaseConfig.MongoDBAtlasReplicaSet.AdditionalProperties.ValueString()), &additionalProperties)
		}
		sourceMongodbV2UpdateMongoDBAtlasReplicaSet = &shared.SourceMongodbV2UpdateMongoDBAtlasReplicaSet{
			AuthSource:           authSource,
			ConnectionString:     connectionString,
			Databases:            databases,
			Password:             password,
			SchemaEnforced:       schemaEnforced,
			Username:             username,
			AdditionalProperties: additionalProperties,
		}
	}
	if sourceMongodbV2UpdateMongoDBAtlasReplicaSet != nil {
		databaseConfig = shared.SourceMongodbV2UpdateClusterType{
			SourceMongodbV2UpdateMongoDBAtlasReplicaSet: sourceMongodbV2UpdateMongoDBAtlasReplicaSet,
		}
	}
	var sourceMongodbV2UpdateSelfManagedReplicaSet *shared.SourceMongodbV2UpdateSelfManagedReplicaSet
	if r.Configuration.DatabaseConfig.SelfManagedReplicaSet != nil {
		authSource1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.IsNull() {
			*authSource1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AuthSource.ValueString()
		} else {
			authSource1 = nil
		}
		var connectionString1 string
		connectionString1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.ConnectionString.ValueString()

		var databases1 []string = []string{}
		for _, databasesItem1 := range r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Databases {
			databases1 = append(databases1, databasesItem1.ValueString())
		}
		password1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.IsNull() {
			*password1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Password.ValueString()
		} else {
			password1 = nil
		}
		schemaEnforced1 := new(bool)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.IsNull() {
			*schemaEnforced1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.SchemaEnforced.ValueBool()
		} else {
			schemaEnforced1 = nil
		}
		username1 := new(string)
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.IsNull() {
			*username1 = r.Configuration.DatabaseConfig.SelfManagedReplicaSet.Username.ValueString()
		} else {
			username1 = nil
		}
		var additionalProperties1 interface{}
		if !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.IsUnknown() && !r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.DatabaseConfig.SelfManagedReplicaSet.AdditionalProperties.ValueString()), &additionalProperties1)
		}
		sourceMongodbV2UpdateSelfManagedReplicaSet = &shared.SourceMongodbV2UpdateSelfManagedReplicaSet{
			AuthSource:           authSource1,
			ConnectionString:     connectionString1,
			Databases:            databases1,
			Password:             password1,
			SchemaEnforced:       schemaEnforced1,
			Username:             username1,
			AdditionalProperties: additionalProperties1,
		}
	}
	if sourceMongodbV2UpdateSelfManagedReplicaSet != nil {
		databaseConfig = shared.SourceMongodbV2UpdateClusterType{
			SourceMongodbV2UpdateSelfManagedReplicaSet: sourceMongodbV2UpdateSelfManagedReplicaSet,
		}
	}
	discoverSampleSize := new(int64)
	if !r.Configuration.DiscoverSampleSize.IsUnknown() && !r.Configuration.DiscoverSampleSize.IsNull() {
		*discoverSampleSize = r.Configuration.DiscoverSampleSize.ValueInt64()
	} else {
		discoverSampleSize = nil
	}
	discoverTimeoutSeconds := new(int64)
	if !r.Configuration.DiscoverTimeoutSeconds.IsUnknown() && !r.Configuration.DiscoverTimeoutSeconds.IsNull() {
		*discoverTimeoutSeconds = r.Configuration.DiscoverTimeoutSeconds.ValueInt64()
	} else {
		discoverTimeoutSeconds = nil
	}
	initialLoadTimeoutHours := new(int64)
	if !r.Configuration.InitialLoadTimeoutHours.IsUnknown() && !r.Configuration.InitialLoadTimeoutHours.IsNull() {
		*initialLoadTimeoutHours = r.Configuration.InitialLoadTimeoutHours.ValueInt64()
	} else {
		initialLoadTimeoutHours = nil
	}
	initialWaitingSeconds := new(int64)
	if !r.Configuration.InitialWaitingSeconds.IsUnknown() && !r.Configuration.InitialWaitingSeconds.IsNull() {
		*initialWaitingSeconds = r.Configuration.InitialWaitingSeconds.ValueInt64()
	} else {
		initialWaitingSeconds = nil
	}
	invalidCdcCursorPositionBehavior := new(shared.SourceMongodbV2UpdateInvalidCDCPositionBehaviorAdvanced)
	if !r.Configuration.InvalidCdcCursorPositionBehavior.IsUnknown() && !r.Configuration.InvalidCdcCursorPositionBehavior.IsNull() {
		*invalidCdcCursorPositionBehavior = shared.SourceMongodbV2UpdateInvalidCDCPositionBehaviorAdvanced(r.Configuration.InvalidCdcCursorPositionBehavior.ValueString())
	} else {
		invalidCdcCursorPositionBehavior = nil
	}
	queueSize := new(int64)
	if !r.Configuration.QueueSize.IsUnknown() && !r.Configuration.QueueSize.IsNull() {
		*queueSize = r.Configuration.QueueSize.ValueInt64()
	} else {
		queueSize = nil
	}
	updateCaptureMode := new(shared.SourceMongodbV2UpdateCaptureModeAdvanced)
	if !r.Configuration.UpdateCaptureMode.IsUnknown() && !r.Configuration.UpdateCaptureMode.IsNull() {
		*updateCaptureMode = shared.SourceMongodbV2UpdateCaptureModeAdvanced(r.Configuration.UpdateCaptureMode.ValueString())
	} else {
		updateCaptureMode = nil
	}
	configuration := shared.SourceMongodbV2Update{
		DatabaseConfig:                   databaseConfig,
		DiscoverSampleSize:               discoverSampleSize,
		DiscoverTimeoutSeconds:           discoverTimeoutSeconds,
		InitialLoadTimeoutHours:          initialLoadTimeoutHours,
		InitialWaitingSeconds:            initialWaitingSeconds,
		InvalidCdcCursorPositionBehavior: invalidCdcCursorPositionBehavior,
		QueueSize:                        queueSize,
		UpdateCaptureMode:                updateCaptureMode,
	}
	out := shared.SourceMongodbV2PutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
