// 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 *SourceSftpResourceModel) ToSharedSourceSftpCreateRequest() *shared.SourceSftpCreateRequest {
	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.SourceSftpAuthentication
	if r.Configuration.Credentials != nil {
		var sourceSftpPasswordAuthentication *shared.SourceSftpPasswordAuthentication
		if r.Configuration.Credentials.PasswordAuthentication != nil {
			var authUserPassword string
			authUserPassword = r.Configuration.Credentials.PasswordAuthentication.AuthUserPassword.ValueString()

			sourceSftpPasswordAuthentication = &shared.SourceSftpPasswordAuthentication{
				AuthUserPassword: authUserPassword,
			}
		}
		if sourceSftpPasswordAuthentication != nil {
			credentials = &shared.SourceSftpAuthentication{
				SourceSftpPasswordAuthentication: sourceSftpPasswordAuthentication,
			}
		}
		var sourceSftpSSHKeyAuthentication *shared.SourceSftpSSHKeyAuthentication
		if r.Configuration.Credentials.SSHKeyAuthentication != nil {
			var authSSHKey string
			authSSHKey = r.Configuration.Credentials.SSHKeyAuthentication.AuthSSHKey.ValueString()

			sourceSftpSSHKeyAuthentication = &shared.SourceSftpSSHKeyAuthentication{
				AuthSSHKey: authSSHKey,
			}
		}
		if sourceSftpSSHKeyAuthentication != nil {
			credentials = &shared.SourceSftpAuthentication{
				SourceSftpSSHKeyAuthentication: sourceSftpSSHKeyAuthentication,
			}
		}
	}
	filePattern := new(string)
	if !r.Configuration.FilePattern.IsUnknown() && !r.Configuration.FilePattern.IsNull() {
		*filePattern = r.Configuration.FilePattern.ValueString()
	} else {
		filePattern = nil
	}
	fileTypes := new(string)
	if !r.Configuration.FileTypes.IsUnknown() && !r.Configuration.FileTypes.IsNull() {
		*fileTypes = r.Configuration.FileTypes.ValueString()
	} else {
		fileTypes = nil
	}
	folderPath := new(string)
	if !r.Configuration.FolderPath.IsUnknown() && !r.Configuration.FolderPath.IsNull() {
		*folderPath = r.Configuration.FolderPath.ValueString()
	} else {
		folderPath = nil
	}
	var host string
	host = r.Configuration.Host.ValueString()

	port := new(int64)
	if !r.Configuration.Port.IsUnknown() && !r.Configuration.Port.IsNull() {
		*port = r.Configuration.Port.ValueInt64()
	} else {
		port = nil
	}
	var user string
	user = r.Configuration.User.ValueString()

	configuration := shared.SourceSftp{
		Credentials: credentials,
		FilePattern: filePattern,
		FileTypes:   fileTypes,
		FolderPath:  folderPath,
		Host:        host,
		Port:        port,
		User:        user,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceSftpCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceSftpResourceModel) 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 *SourceSftpResourceModel) ToSharedSourceSftpPutRequest() *shared.SourceSftpPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var credentials *shared.SourceSftpUpdateAuthentication
	if r.Configuration.Credentials != nil {
		var sourceSftpUpdatePasswordAuthentication *shared.SourceSftpUpdatePasswordAuthentication
		if r.Configuration.Credentials.PasswordAuthentication != nil {
			var authUserPassword string
			authUserPassword = r.Configuration.Credentials.PasswordAuthentication.AuthUserPassword.ValueString()

			sourceSftpUpdatePasswordAuthentication = &shared.SourceSftpUpdatePasswordAuthentication{
				AuthUserPassword: authUserPassword,
			}
		}
		if sourceSftpUpdatePasswordAuthentication != nil {
			credentials = &shared.SourceSftpUpdateAuthentication{
				SourceSftpUpdatePasswordAuthentication: sourceSftpUpdatePasswordAuthentication,
			}
		}
		var sourceSftpUpdateSSHKeyAuthentication *shared.SourceSftpUpdateSSHKeyAuthentication
		if r.Configuration.Credentials.SSHKeyAuthentication != nil {
			var authSSHKey string
			authSSHKey = r.Configuration.Credentials.SSHKeyAuthentication.AuthSSHKey.ValueString()

			sourceSftpUpdateSSHKeyAuthentication = &shared.SourceSftpUpdateSSHKeyAuthentication{
				AuthSSHKey: authSSHKey,
			}
		}
		if sourceSftpUpdateSSHKeyAuthentication != nil {
			credentials = &shared.SourceSftpUpdateAuthentication{
				SourceSftpUpdateSSHKeyAuthentication: sourceSftpUpdateSSHKeyAuthentication,
			}
		}
	}
	filePattern := new(string)
	if !r.Configuration.FilePattern.IsUnknown() && !r.Configuration.FilePattern.IsNull() {
		*filePattern = r.Configuration.FilePattern.ValueString()
	} else {
		filePattern = nil
	}
	fileTypes := new(string)
	if !r.Configuration.FileTypes.IsUnknown() && !r.Configuration.FileTypes.IsNull() {
		*fileTypes = r.Configuration.FileTypes.ValueString()
	} else {
		fileTypes = nil
	}
	folderPath := new(string)
	if !r.Configuration.FolderPath.IsUnknown() && !r.Configuration.FolderPath.IsNull() {
		*folderPath = r.Configuration.FolderPath.ValueString()
	} else {
		folderPath = nil
	}
	var host string
	host = r.Configuration.Host.ValueString()

	port := new(int64)
	if !r.Configuration.Port.IsUnknown() && !r.Configuration.Port.IsNull() {
		*port = r.Configuration.Port.ValueInt64()
	} else {
		port = nil
	}
	var user string
	user = r.Configuration.User.ValueString()

	configuration := shared.SourceSftpUpdate{
		Credentials: credentials,
		FilePattern: filePattern,
		FileTypes:   fileTypes,
		FolderPath:  folderPath,
		Host:        host,
		Port:        port,
		User:        user,
	}
	out := shared.SourceSftpPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
