// 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 *DestinationMongodbResourceModel) ToSharedDestinationMongodbCreateRequest() *shared.DestinationMongodbCreateRequest {
	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 authType shared.AuthorizationType
	var destinationMongodbNone *shared.DestinationMongodbNone
	if r.Configuration.AuthType.None != nil {
		destinationMongodbNone = &shared.DestinationMongodbNone{}
	}
	if destinationMongodbNone != nil {
		authType = shared.AuthorizationType{
			DestinationMongodbNone: destinationMongodbNone,
		}
	}
	var loginPassword *shared.LoginPassword
	if r.Configuration.AuthType.LoginPassword != nil {
		var password string
		password = r.Configuration.AuthType.LoginPassword.Password.ValueString()

		var username string
		username = r.Configuration.AuthType.LoginPassword.Username.ValueString()

		loginPassword = &shared.LoginPassword{
			Password: password,
			Username: username,
		}
	}
	if loginPassword != nil {
		authType = shared.AuthorizationType{
			LoginPassword: loginPassword,
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	var instanceType *shared.MongoDbInstanceType
	if r.Configuration.InstanceType != nil {
		var standaloneMongoDbInstance *shared.StandaloneMongoDbInstance
		if r.Configuration.InstanceType.StandaloneMongoDbInstance != nil {
			var host string
			host = r.Configuration.InstanceType.StandaloneMongoDbInstance.Host.ValueString()

			instance := new(shared.Instance)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.IsNull() {
				*instance = shared.Instance(r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.ValueString())
			} else {
				instance = nil
			}
			port := new(int64)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.IsNull() {
				*port = r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.ValueInt64()
			} else {
				port = nil
			}
			tls := new(bool)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.IsNull() {
				*tls = r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.ValueBool()
			} else {
				tls = nil
			}
			standaloneMongoDbInstance = &shared.StandaloneMongoDbInstance{
				Host:     host,
				Instance: instance,
				Port:     port,
				TLS:      tls,
			}
		}
		if standaloneMongoDbInstance != nil {
			instanceType = &shared.MongoDbInstanceType{
				StandaloneMongoDbInstance: standaloneMongoDbInstance,
			}
		}
		var replicaSet *shared.ReplicaSet
		if r.Configuration.InstanceType.ReplicaSet != nil {
			instance1 := new(shared.DestinationMongodbInstance)
			if !r.Configuration.InstanceType.ReplicaSet.Instance.IsUnknown() && !r.Configuration.InstanceType.ReplicaSet.Instance.IsNull() {
				*instance1 = shared.DestinationMongodbInstance(r.Configuration.InstanceType.ReplicaSet.Instance.ValueString())
			} else {
				instance1 = nil
			}
			replicaSet1 := new(string)
			if !r.Configuration.InstanceType.ReplicaSet.ReplicaSet.IsUnknown() && !r.Configuration.InstanceType.ReplicaSet.ReplicaSet.IsNull() {
				*replicaSet1 = r.Configuration.InstanceType.ReplicaSet.ReplicaSet.ValueString()
			} else {
				replicaSet1 = nil
			}
			var serverAddresses string
			serverAddresses = r.Configuration.InstanceType.ReplicaSet.ServerAddresses.ValueString()

			replicaSet = &shared.ReplicaSet{
				Instance:        instance1,
				ReplicaSet:      replicaSet1,
				ServerAddresses: serverAddresses,
			}
		}
		if replicaSet != nil {
			instanceType = &shared.MongoDbInstanceType{
				ReplicaSet: replicaSet,
			}
		}
		var mongoDBAtlas *shared.MongoDBAtlas
		if r.Configuration.InstanceType.MongoDBAtlas != nil {
			var clusterURL string
			clusterURL = r.Configuration.InstanceType.MongoDBAtlas.ClusterURL.ValueString()

			instance2 := new(shared.DestinationMongodbSchemasInstance)
			if !r.Configuration.InstanceType.MongoDBAtlas.Instance.IsUnknown() && !r.Configuration.InstanceType.MongoDBAtlas.Instance.IsNull() {
				*instance2 = shared.DestinationMongodbSchemasInstance(r.Configuration.InstanceType.MongoDBAtlas.Instance.ValueString())
			} else {
				instance2 = nil
			}
			mongoDBAtlas = &shared.MongoDBAtlas{
				ClusterURL: clusterURL,
				Instance:   instance2,
			}
		}
		if mongoDBAtlas != nil {
			instanceType = &shared.MongoDbInstanceType{
				MongoDBAtlas: mongoDBAtlas,
			}
		}
	}
	var tunnelMethod *shared.DestinationMongodbSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var destinationMongodbNoTunnel *shared.DestinationMongodbNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			destinationMongodbNoTunnel = &shared.DestinationMongodbNoTunnel{}
		}
		if destinationMongodbNoTunnel != nil {
			tunnelMethod = &shared.DestinationMongodbSSHTunnelMethod{
				DestinationMongodbNoTunnel: destinationMongodbNoTunnel,
			}
		}
		var destinationMongodbSSHKeyAuthentication *shared.DestinationMongodbSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			destinationMongodbSSHKeyAuthentication = &shared.DestinationMongodbSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if destinationMongodbSSHKeyAuthentication != nil {
			tunnelMethod = &shared.DestinationMongodbSSHTunnelMethod{
				DestinationMongodbSSHKeyAuthentication: destinationMongodbSSHKeyAuthentication,
			}
		}
		var destinationMongodbPasswordAuthentication *shared.DestinationMongodbPasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			destinationMongodbPasswordAuthentication = &shared.DestinationMongodbPasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if destinationMongodbPasswordAuthentication != nil {
			tunnelMethod = &shared.DestinationMongodbSSHTunnelMethod{
				DestinationMongodbPasswordAuthentication: destinationMongodbPasswordAuthentication,
			}
		}
	}
	configuration := shared.DestinationMongodb{
		AuthType:     authType,
		Database:     database,
		InstanceType: instanceType,
		TunnelMethod: tunnelMethod,
	}
	out := shared.DestinationMongodbCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationMongodbResourceModel) 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 *DestinationMongodbResourceModel) ToSharedDestinationMongodbPutRequest() *shared.DestinationMongodbPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var authType shared.DestinationMongodbUpdateAuthorizationType
	var destinationMongodbUpdateNone *shared.DestinationMongodbUpdateNone
	if r.Configuration.AuthType.None != nil {
		destinationMongodbUpdateNone = &shared.DestinationMongodbUpdateNone{}
	}
	if destinationMongodbUpdateNone != nil {
		authType = shared.DestinationMongodbUpdateAuthorizationType{
			DestinationMongodbUpdateNone: destinationMongodbUpdateNone,
		}
	}
	var destinationMongodbUpdateLoginPassword *shared.DestinationMongodbUpdateLoginPassword
	if r.Configuration.AuthType.LoginPassword != nil {
		var password string
		password = r.Configuration.AuthType.LoginPassword.Password.ValueString()

		var username string
		username = r.Configuration.AuthType.LoginPassword.Username.ValueString()

		destinationMongodbUpdateLoginPassword = &shared.DestinationMongodbUpdateLoginPassword{
			Password: password,
			Username: username,
		}
	}
	if destinationMongodbUpdateLoginPassword != nil {
		authType = shared.DestinationMongodbUpdateAuthorizationType{
			DestinationMongodbUpdateLoginPassword: destinationMongodbUpdateLoginPassword,
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	var instanceType *shared.DestinationMongodbUpdateMongoDbInstanceType
	if r.Configuration.InstanceType != nil {
		var destinationMongodbUpdateStandaloneMongoDbInstance *shared.DestinationMongodbUpdateStandaloneMongoDbInstance
		if r.Configuration.InstanceType.StandaloneMongoDbInstance != nil {
			var host string
			host = r.Configuration.InstanceType.StandaloneMongoDbInstance.Host.ValueString()

			instance := new(shared.DestinationMongodbUpdateInstance)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.IsNull() {
				*instance = shared.DestinationMongodbUpdateInstance(r.Configuration.InstanceType.StandaloneMongoDbInstance.Instance.ValueString())
			} else {
				instance = nil
			}
			port := new(int64)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.IsNull() {
				*port = r.Configuration.InstanceType.StandaloneMongoDbInstance.Port.ValueInt64()
			} else {
				port = nil
			}
			tls := new(bool)
			if !r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.IsUnknown() && !r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.IsNull() {
				*tls = r.Configuration.InstanceType.StandaloneMongoDbInstance.TLS.ValueBool()
			} else {
				tls = nil
			}
			destinationMongodbUpdateStandaloneMongoDbInstance = &shared.DestinationMongodbUpdateStandaloneMongoDbInstance{
				Host:     host,
				Instance: instance,
				Port:     port,
				TLS:      tls,
			}
		}
		if destinationMongodbUpdateStandaloneMongoDbInstance != nil {
			instanceType = &shared.DestinationMongodbUpdateMongoDbInstanceType{
				DestinationMongodbUpdateStandaloneMongoDbInstance: destinationMongodbUpdateStandaloneMongoDbInstance,
			}
		}
		var destinationMongodbUpdateReplicaSet *shared.DestinationMongodbUpdateReplicaSet
		if r.Configuration.InstanceType.ReplicaSet != nil {
			instance1 := new(shared.DestinationMongodbUpdateSchemasInstance)
			if !r.Configuration.InstanceType.ReplicaSet.Instance.IsUnknown() && !r.Configuration.InstanceType.ReplicaSet.Instance.IsNull() {
				*instance1 = shared.DestinationMongodbUpdateSchemasInstance(r.Configuration.InstanceType.ReplicaSet.Instance.ValueString())
			} else {
				instance1 = nil
			}
			replicaSet := new(string)
			if !r.Configuration.InstanceType.ReplicaSet.ReplicaSet.IsUnknown() && !r.Configuration.InstanceType.ReplicaSet.ReplicaSet.IsNull() {
				*replicaSet = r.Configuration.InstanceType.ReplicaSet.ReplicaSet.ValueString()
			} else {
				replicaSet = nil
			}
			var serverAddresses string
			serverAddresses = r.Configuration.InstanceType.ReplicaSet.ServerAddresses.ValueString()

			destinationMongodbUpdateReplicaSet = &shared.DestinationMongodbUpdateReplicaSet{
				Instance:        instance1,
				ReplicaSet:      replicaSet,
				ServerAddresses: serverAddresses,
			}
		}
		if destinationMongodbUpdateReplicaSet != nil {
			instanceType = &shared.DestinationMongodbUpdateMongoDbInstanceType{
				DestinationMongodbUpdateReplicaSet: destinationMongodbUpdateReplicaSet,
			}
		}
		var destinationMongodbUpdateMongoDBAtlas *shared.DestinationMongodbUpdateMongoDBAtlas
		if r.Configuration.InstanceType.MongoDBAtlas != nil {
			var clusterURL string
			clusterURL = r.Configuration.InstanceType.MongoDBAtlas.ClusterURL.ValueString()

			instance2 := new(shared.DestinationMongodbUpdateSchemasInstanceTypeInstance)
			if !r.Configuration.InstanceType.MongoDBAtlas.Instance.IsUnknown() && !r.Configuration.InstanceType.MongoDBAtlas.Instance.IsNull() {
				*instance2 = shared.DestinationMongodbUpdateSchemasInstanceTypeInstance(r.Configuration.InstanceType.MongoDBAtlas.Instance.ValueString())
			} else {
				instance2 = nil
			}
			destinationMongodbUpdateMongoDBAtlas = &shared.DestinationMongodbUpdateMongoDBAtlas{
				ClusterURL: clusterURL,
				Instance:   instance2,
			}
		}
		if destinationMongodbUpdateMongoDBAtlas != nil {
			instanceType = &shared.DestinationMongodbUpdateMongoDbInstanceType{
				DestinationMongodbUpdateMongoDBAtlas: destinationMongodbUpdateMongoDBAtlas,
			}
		}
	}
	var tunnelMethod *shared.DestinationMongodbUpdateSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var destinationMongodbUpdateNoTunnel *shared.DestinationMongodbUpdateNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			destinationMongodbUpdateNoTunnel = &shared.DestinationMongodbUpdateNoTunnel{}
		}
		if destinationMongodbUpdateNoTunnel != nil {
			tunnelMethod = &shared.DestinationMongodbUpdateSSHTunnelMethod{
				DestinationMongodbUpdateNoTunnel: destinationMongodbUpdateNoTunnel,
			}
		}
		var destinationMongodbUpdateSSHKeyAuthentication *shared.DestinationMongodbUpdateSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			destinationMongodbUpdateSSHKeyAuthentication = &shared.DestinationMongodbUpdateSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if destinationMongodbUpdateSSHKeyAuthentication != nil {
			tunnelMethod = &shared.DestinationMongodbUpdateSSHTunnelMethod{
				DestinationMongodbUpdateSSHKeyAuthentication: destinationMongodbUpdateSSHKeyAuthentication,
			}
		}
		var destinationMongodbUpdatePasswordAuthentication *shared.DestinationMongodbUpdatePasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			destinationMongodbUpdatePasswordAuthentication = &shared.DestinationMongodbUpdatePasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if destinationMongodbUpdatePasswordAuthentication != nil {
			tunnelMethod = &shared.DestinationMongodbUpdateSSHTunnelMethod{
				DestinationMongodbUpdatePasswordAuthentication: destinationMongodbUpdatePasswordAuthentication,
			}
		}
	}
	configuration := shared.DestinationMongodbUpdate{
		AuthType:     authType,
		Database:     database,
		InstanceType: instanceType,
		TunnelMethod: tunnelMethod,
	}
	out := shared.DestinationMongodbPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
