// 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 *SourceSnowflakeResourceModel) ToSharedSourceSnowflakeCreateRequest() *shared.SourceSnowflakeCreateRequest {
	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.SourceSnowflakeAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var sourceSnowflakeOAuth20 *shared.SourceSnowflakeOAuth20
		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
			}
			var clientID string
			clientID = r.Configuration.Credentials.OAuth20.ClientID.ValueString()

			var clientSecret string
			clientSecret = r.Configuration.Credentials.OAuth20.ClientSecret.ValueString()

			refreshToken := new(string)
			if !r.Configuration.Credentials.OAuth20.RefreshToken.IsUnknown() && !r.Configuration.Credentials.OAuth20.RefreshToken.IsNull() {
				*refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()
			} else {
				refreshToken = nil
			}
			sourceSnowflakeOAuth20 = &shared.SourceSnowflakeOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
				RefreshToken: refreshToken,
			}
		}
		if sourceSnowflakeOAuth20 != nil {
			credentials = &shared.SourceSnowflakeAuthorizationMethod{
				SourceSnowflakeOAuth20: sourceSnowflakeOAuth20,
			}
		}
		var keyPairAuthentication *shared.KeyPairAuthentication
		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
			}
			var username string
			username = r.Configuration.Credentials.KeyPairAuthentication.Username.ValueString()

			keyPairAuthentication = &shared.KeyPairAuthentication{
				PrivateKey:         privateKey,
				PrivateKeyPassword: privateKeyPassword,
				Username:           username,
			}
		}
		if keyPairAuthentication != nil {
			credentials = &shared.SourceSnowflakeAuthorizationMethod{
				KeyPairAuthentication: keyPairAuthentication,
			}
		}
		var usernameAndPassword *shared.UsernameAndPassword
		if r.Configuration.Credentials.UsernameAndPassword != nil {
			var password string
			password = r.Configuration.Credentials.UsernameAndPassword.Password.ValueString()

			var username1 string
			username1 = r.Configuration.Credentials.UsernameAndPassword.Username.ValueString()

			usernameAndPassword = &shared.UsernameAndPassword{
				Password: password,
				Username: username1,
			}
		}
		if usernameAndPassword != nil {
			credentials = &shared.SourceSnowflakeAuthorizationMethod{
				UsernameAndPassword: usernameAndPassword,
			}
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	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
	}
	var role string
	role = r.Configuration.Role.ValueString()

	schema := new(string)
	if !r.Configuration.Schema.IsUnknown() && !r.Configuration.Schema.IsNull() {
		*schema = r.Configuration.Schema.ValueString()
	} else {
		schema = nil
	}
	var warehouse string
	warehouse = r.Configuration.Warehouse.ValueString()

	configuration := shared.SourceSnowflake{
		Credentials:   credentials,
		Database:      database,
		Host:          host,
		JdbcURLParams: jdbcURLParams,
		Role:          role,
		Schema:        schema,
		Warehouse:     warehouse,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceSnowflakeCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceSnowflakeResourceModel) 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 *SourceSnowflakeResourceModel) ToSharedSourceSnowflakePutRequest() *shared.SourceSnowflakePutRequest {
	var name string
	name = r.Name.ValueString()

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

	var credentials *shared.SourceSnowflakeUpdateAuthorizationMethod
	if r.Configuration.Credentials != nil {
		var sourceSnowflakeUpdateOAuth20 *shared.SourceSnowflakeUpdateOAuth20
		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
			}
			var clientID string
			clientID = r.Configuration.Credentials.OAuth20.ClientID.ValueString()

			var clientSecret string
			clientSecret = r.Configuration.Credentials.OAuth20.ClientSecret.ValueString()

			refreshToken := new(string)
			if !r.Configuration.Credentials.OAuth20.RefreshToken.IsUnknown() && !r.Configuration.Credentials.OAuth20.RefreshToken.IsNull() {
				*refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()
			} else {
				refreshToken = nil
			}
			sourceSnowflakeUpdateOAuth20 = &shared.SourceSnowflakeUpdateOAuth20{
				AccessToken:  accessToken,
				ClientID:     clientID,
				ClientSecret: clientSecret,
				RefreshToken: refreshToken,
			}
		}
		if sourceSnowflakeUpdateOAuth20 != nil {
			credentials = &shared.SourceSnowflakeUpdateAuthorizationMethod{
				SourceSnowflakeUpdateOAuth20: sourceSnowflakeUpdateOAuth20,
			}
		}
		var sourceSnowflakeUpdateKeyPairAuthentication *shared.SourceSnowflakeUpdateKeyPairAuthentication
		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
			}
			var username string
			username = r.Configuration.Credentials.KeyPairAuthentication.Username.ValueString()

			sourceSnowflakeUpdateKeyPairAuthentication = &shared.SourceSnowflakeUpdateKeyPairAuthentication{
				PrivateKey:         privateKey,
				PrivateKeyPassword: privateKeyPassword,
				Username:           username,
			}
		}
		if sourceSnowflakeUpdateKeyPairAuthentication != nil {
			credentials = &shared.SourceSnowflakeUpdateAuthorizationMethod{
				SourceSnowflakeUpdateKeyPairAuthentication: sourceSnowflakeUpdateKeyPairAuthentication,
			}
		}
		var sourceSnowflakeUpdateUsernameAndPassword *shared.SourceSnowflakeUpdateUsernameAndPassword
		if r.Configuration.Credentials.UsernameAndPassword != nil {
			var password string
			password = r.Configuration.Credentials.UsernameAndPassword.Password.ValueString()

			var username1 string
			username1 = r.Configuration.Credentials.UsernameAndPassword.Username.ValueString()

			sourceSnowflakeUpdateUsernameAndPassword = &shared.SourceSnowflakeUpdateUsernameAndPassword{
				Password: password,
				Username: username1,
			}
		}
		if sourceSnowflakeUpdateUsernameAndPassword != nil {
			credentials = &shared.SourceSnowflakeUpdateAuthorizationMethod{
				SourceSnowflakeUpdateUsernameAndPassword: sourceSnowflakeUpdateUsernameAndPassword,
			}
		}
	}
	var database string
	database = r.Configuration.Database.ValueString()

	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
	}
	var role string
	role = r.Configuration.Role.ValueString()

	schema := new(string)
	if !r.Configuration.Schema.IsUnknown() && !r.Configuration.Schema.IsNull() {
		*schema = r.Configuration.Schema.ValueString()
	} else {
		schema = nil
	}
	var warehouse string
	warehouse = r.Configuration.Warehouse.ValueString()

	configuration := shared.SourceSnowflakeUpdate{
		Credentials:   credentials,
		Database:      database,
		Host:          host,
		JdbcURLParams: jdbcURLParams,
		Role:          role,
		Schema:        schema,
		Warehouse:     warehouse,
	}
	out := shared.SourceSnowflakePutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
