// 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 *DestinationSnowflakeResourceModel) ToSharedDestinationSnowflakeCreateRequest() *shared.DestinationSnowflakeCreateRequest {
	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 credentials *shared.DestinationSnowflakeAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var destinationSnowflakeKeyPairAuthentication *shared.DestinationSnowflakeKeyPairAuthentication
		if r.Configuration.Credentials.KeyPairAuthentication != nil {
			var privateKey string
			privateKey = r.Configuration.Credentials.KeyPairAuthentication.PrivateKey.ValueString()

			privateKeyPassword := new(string)
			if !r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.IsUnknown() && !r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.IsNull() {
				*privateKeyPassword = r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.ValueString()
			} else {
				privateKeyPassword = nil
			}
			destinationSnowflakeKeyPairAuthentication = &shared.DestinationSnowflakeKeyPairAuthentication{
				PrivateKey:         privateKey,
				PrivateKeyPassword: privateKeyPassword,
			}
		}
		if destinationSnowflakeKeyPairAuthentication != nil {
			credentials = &shared.DestinationSnowflakeAuthorizationMethod{
				DestinationSnowflakeKeyPairAuthentication: destinationSnowflakeKeyPairAuthentication,
			}
		}
		var destinationSnowflakeUsernameAndPassword *shared.DestinationSnowflakeUsernameAndPassword
		if r.Configuration.Credentials.UsernameAndPassword != nil {
			var password string
			password = r.Configuration.Credentials.UsernameAndPassword.Password.ValueString()

			destinationSnowflakeUsernameAndPassword = &shared.DestinationSnowflakeUsernameAndPassword{
				Password: password,
			}
		}
		if destinationSnowflakeUsernameAndPassword != nil {
			credentials = &shared.DestinationSnowflakeAuthorizationMethod{
				DestinationSnowflakeUsernameAndPassword: destinationSnowflakeUsernameAndPassword,
			}
		}
		var destinationSnowflakeOAuth20 *shared.DestinationSnowflakeOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			var accessToken string
			accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()

			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
			}
			var refreshToken string
			refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()

			destinationSnowflakeOAuth20 = &shared.DestinationSnowflakeOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
				RefreshToken: refreshToken,
			}
		}
		if destinationSnowflakeOAuth20 != nil {
			credentials = &shared.DestinationSnowflakeAuthorizationMethod{
				DestinationSnowflakeOAuth20: destinationSnowflakeOAuth20,
			}
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	disableTypeDedupe := new(bool)
	if !r.Configuration.DisableTypeDedupe.IsUnknown() && !r.Configuration.DisableTypeDedupe.IsNull() {
		*disableTypeDedupe = r.Configuration.DisableTypeDedupe.ValueBool()
	} else {
		disableTypeDedupe = nil
	}
	var host string
	host = r.Configuration.Host.ValueString()

	jdbcURLParams := new(string)
	if !r.Configuration.JdbcURLParams.IsUnknown() && !r.Configuration.JdbcURLParams.IsNull() {
		*jdbcURLParams = r.Configuration.JdbcURLParams.ValueString()
	} else {
		jdbcURLParams = nil
	}
	rawDataSchema := new(string)
	if !r.Configuration.RawDataSchema.IsUnknown() && !r.Configuration.RawDataSchema.IsNull() {
		*rawDataSchema = r.Configuration.RawDataSchema.ValueString()
	} else {
		rawDataSchema = nil
	}
	retentionPeriodDays := new(int64)
	if !r.Configuration.RetentionPeriodDays.IsUnknown() && !r.Configuration.RetentionPeriodDays.IsNull() {
		*retentionPeriodDays = r.Configuration.RetentionPeriodDays.ValueInt64()
	} else {
		retentionPeriodDays = nil
	}
	var role string
	role = r.Configuration.Role.ValueString()

	var schema string
	schema = r.Configuration.Schema.ValueString()

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

	var warehouse string
	warehouse = r.Configuration.Warehouse.ValueString()

	configuration := shared.DestinationSnowflake{
		Credentials:         credentials,
		Database:            database,
		DisableTypeDedupe:   disableTypeDedupe,
		Host:                host,
		JdbcURLParams:       jdbcURLParams,
		RawDataSchema:       rawDataSchema,
		RetentionPeriodDays: retentionPeriodDays,
		Role:                role,
		Schema:              schema,
		UseMergeForUpsert:   useMergeForUpsert,
		Username:            username,
		Warehouse:           warehouse,
	}
	out := shared.DestinationSnowflakeCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationSnowflakeResourceModel) 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 *DestinationSnowflakeResourceModel) ToSharedDestinationSnowflakePutRequest() *shared.DestinationSnowflakePutRequest {
	var name string
	name = r.Name.ValueString()

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

	var credentials *shared.DestinationSnowflakeUpdateAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var destinationSnowflakeUpdateKeyPairAuthentication *shared.DestinationSnowflakeUpdateKeyPairAuthentication
		if r.Configuration.Credentials.KeyPairAuthentication != nil {
			var privateKey string
			privateKey = r.Configuration.Credentials.KeyPairAuthentication.PrivateKey.ValueString()

			privateKeyPassword := new(string)
			if !r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.IsUnknown() && !r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.IsNull() {
				*privateKeyPassword = r.Configuration.Credentials.KeyPairAuthentication.PrivateKeyPassword.ValueString()
			} else {
				privateKeyPassword = nil
			}
			destinationSnowflakeUpdateKeyPairAuthentication = &shared.DestinationSnowflakeUpdateKeyPairAuthentication{
				PrivateKey:         privateKey,
				PrivateKeyPassword: privateKeyPassword,
			}
		}
		if destinationSnowflakeUpdateKeyPairAuthentication != nil {
			credentials = &shared.DestinationSnowflakeUpdateAuthorizationMethod{
				DestinationSnowflakeUpdateKeyPairAuthentication: destinationSnowflakeUpdateKeyPairAuthentication,
			}
		}
		var destinationSnowflakeUpdateUsernameAndPassword *shared.DestinationSnowflakeUpdateUsernameAndPassword
		if r.Configuration.Credentials.UsernameAndPassword != nil {
			var password string
			password = r.Configuration.Credentials.UsernameAndPassword.Password.ValueString()

			destinationSnowflakeUpdateUsernameAndPassword = &shared.DestinationSnowflakeUpdateUsernameAndPassword{
				Password: password,
			}
		}
		if destinationSnowflakeUpdateUsernameAndPassword != nil {
			credentials = &shared.DestinationSnowflakeUpdateAuthorizationMethod{
				DestinationSnowflakeUpdateUsernameAndPassword: destinationSnowflakeUpdateUsernameAndPassword,
			}
		}
		var destinationSnowflakeUpdateOAuth20 *shared.DestinationSnowflakeUpdateOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			var accessToken string
			accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()

			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
			}
			var refreshToken string
			refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()

			destinationSnowflakeUpdateOAuth20 = &shared.DestinationSnowflakeUpdateOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
				RefreshToken: refreshToken,
			}
		}
		if destinationSnowflakeUpdateOAuth20 != nil {
			credentials = &shared.DestinationSnowflakeUpdateAuthorizationMethod{
				DestinationSnowflakeUpdateOAuth20: destinationSnowflakeUpdateOAuth20,
			}
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	disableTypeDedupe := new(bool)
	if !r.Configuration.DisableTypeDedupe.IsUnknown() && !r.Configuration.DisableTypeDedupe.IsNull() {
		*disableTypeDedupe = r.Configuration.DisableTypeDedupe.ValueBool()
	} else {
		disableTypeDedupe = nil
	}
	var host string
	host = r.Configuration.Host.ValueString()

	jdbcURLParams := new(string)
	if !r.Configuration.JdbcURLParams.IsUnknown() && !r.Configuration.JdbcURLParams.IsNull() {
		*jdbcURLParams = r.Configuration.JdbcURLParams.ValueString()
	} else {
		jdbcURLParams = nil
	}
	rawDataSchema := new(string)
	if !r.Configuration.RawDataSchema.IsUnknown() && !r.Configuration.RawDataSchema.IsNull() {
		*rawDataSchema = r.Configuration.RawDataSchema.ValueString()
	} else {
		rawDataSchema = nil
	}
	retentionPeriodDays := new(int64)
	if !r.Configuration.RetentionPeriodDays.IsUnknown() && !r.Configuration.RetentionPeriodDays.IsNull() {
		*retentionPeriodDays = r.Configuration.RetentionPeriodDays.ValueInt64()
	} else {
		retentionPeriodDays = nil
	}
	var role string
	role = r.Configuration.Role.ValueString()

	var schema string
	schema = r.Configuration.Schema.ValueString()

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

	var warehouse string
	warehouse = r.Configuration.Warehouse.ValueString()

	configuration := shared.DestinationSnowflakeUpdate{
		Credentials:         credentials,
		Database:            database,
		DisableTypeDedupe:   disableTypeDedupe,
		Host:                host,
		JdbcURLParams:       jdbcURLParams,
		RawDataSchema:       rawDataSchema,
		RetentionPeriodDays: retentionPeriodDays,
		Role:                role,
		Schema:              schema,
		UseMergeForUpsert:   useMergeForUpsert,
		Username:            username,
		Warehouse:           warehouse,
	}
	out := shared.DestinationSnowflakePutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
