// 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 *SourceDynamodbResourceModel) ToSharedSourceDynamodbCreateRequest() *shared.SourceDynamodbCreateRequest {
	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.Credentials
	if r.Configuration.Credentials != nil {
		var authenticateViaAccessKeys *shared.AuthenticateViaAccessKeys
		if r.Configuration.Credentials.AuthenticateViaAccessKeys != nil {
			var accessKeyID string
			accessKeyID = r.Configuration.Credentials.AuthenticateViaAccessKeys.AccessKeyID.ValueString()

			var secretAccessKey string
			secretAccessKey = r.Configuration.Credentials.AuthenticateViaAccessKeys.SecretAccessKey.ValueString()

			var additionalProperties interface{}
			if !r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.IsUnknown() && !r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.ValueString()), &additionalProperties)
			}
			authenticateViaAccessKeys = &shared.AuthenticateViaAccessKeys{
				AccessKeyID:          accessKeyID,
				SecretAccessKey:      secretAccessKey,
				AdditionalProperties: additionalProperties,
			}
		}
		if authenticateViaAccessKeys != nil {
			credentials = &shared.Credentials{
				AuthenticateViaAccessKeys: authenticateViaAccessKeys,
			}
		}
		var roleBasedAuthentication *shared.RoleBasedAuthentication
		if r.Configuration.Credentials.RoleBasedAuthentication != nil {
			var additionalProperties1 interface{}
			if !r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.IsUnknown() && !r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.ValueString()), &additionalProperties1)
			}
			roleBasedAuthentication = &shared.RoleBasedAuthentication{
				AdditionalProperties: additionalProperties1,
			}
		}
		if roleBasedAuthentication != nil {
			credentials = &shared.Credentials{
				RoleBasedAuthentication: roleBasedAuthentication,
			}
		}
	}
	endpoint := new(string)
	if !r.Configuration.Endpoint.IsUnknown() && !r.Configuration.Endpoint.IsNull() {
		*endpoint = r.Configuration.Endpoint.ValueString()
	} else {
		endpoint = nil
	}
	ignoreMissingReadPermissionsTables := new(bool)
	if !r.Configuration.IgnoreMissingReadPermissionsTables.IsUnknown() && !r.Configuration.IgnoreMissingReadPermissionsTables.IsNull() {
		*ignoreMissingReadPermissionsTables = r.Configuration.IgnoreMissingReadPermissionsTables.ValueBool()
	} else {
		ignoreMissingReadPermissionsTables = nil
	}
	region := new(shared.DynamodbRegion)
	if !r.Configuration.Region.IsUnknown() && !r.Configuration.Region.IsNull() {
		*region = shared.DynamodbRegion(r.Configuration.Region.ValueString())
	} else {
		region = nil
	}
	reservedAttributeNames := new(string)
	if !r.Configuration.ReservedAttributeNames.IsUnknown() && !r.Configuration.ReservedAttributeNames.IsNull() {
		*reservedAttributeNames = r.Configuration.ReservedAttributeNames.ValueString()
	} else {
		reservedAttributeNames = nil
	}
	configuration := shared.SourceDynamodb{
		Credentials:                        credentials,
		Endpoint:                           endpoint,
		IgnoreMissingReadPermissionsTables: ignoreMissingReadPermissionsTables,
		Region:                             region,
		ReservedAttributeNames:             reservedAttributeNames,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceDynamodbCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceDynamodbResourceModel) 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 *SourceDynamodbResourceModel) ToSharedSourceDynamodbPutRequest() *shared.SourceDynamodbPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var credentials *shared.SourceDynamodbUpdateCredentials
	if r.Configuration.Credentials != nil {
		var sourceDynamodbUpdateAuthenticateViaAccessKeys *shared.SourceDynamodbUpdateAuthenticateViaAccessKeys
		if r.Configuration.Credentials.AuthenticateViaAccessKeys != nil {
			var accessKeyID string
			accessKeyID = r.Configuration.Credentials.AuthenticateViaAccessKeys.AccessKeyID.ValueString()

			var secretAccessKey string
			secretAccessKey = r.Configuration.Credentials.AuthenticateViaAccessKeys.SecretAccessKey.ValueString()

			var additionalProperties interface{}
			if !r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.IsUnknown() && !r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.Credentials.AuthenticateViaAccessKeys.AdditionalProperties.ValueString()), &additionalProperties)
			}
			sourceDynamodbUpdateAuthenticateViaAccessKeys = &shared.SourceDynamodbUpdateAuthenticateViaAccessKeys{
				AccessKeyID:          accessKeyID,
				SecretAccessKey:      secretAccessKey,
				AdditionalProperties: additionalProperties,
			}
		}
		if sourceDynamodbUpdateAuthenticateViaAccessKeys != nil {
			credentials = &shared.SourceDynamodbUpdateCredentials{
				SourceDynamodbUpdateAuthenticateViaAccessKeys: sourceDynamodbUpdateAuthenticateViaAccessKeys,
			}
		}
		var sourceDynamodbUpdateRoleBasedAuthentication *shared.SourceDynamodbUpdateRoleBasedAuthentication
		if r.Configuration.Credentials.RoleBasedAuthentication != nil {
			var additionalProperties1 interface{}
			if !r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.IsUnknown() && !r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.Credentials.RoleBasedAuthentication.AdditionalProperties.ValueString()), &additionalProperties1)
			}
			sourceDynamodbUpdateRoleBasedAuthentication = &shared.SourceDynamodbUpdateRoleBasedAuthentication{
				AdditionalProperties: additionalProperties1,
			}
		}
		if sourceDynamodbUpdateRoleBasedAuthentication != nil {
			credentials = &shared.SourceDynamodbUpdateCredentials{
				SourceDynamodbUpdateRoleBasedAuthentication: sourceDynamodbUpdateRoleBasedAuthentication,
			}
		}
	}
	endpoint := new(string)
	if !r.Configuration.Endpoint.IsUnknown() && !r.Configuration.Endpoint.IsNull() {
		*endpoint = r.Configuration.Endpoint.ValueString()
	} else {
		endpoint = nil
	}
	ignoreMissingReadPermissionsTables := new(bool)
	if !r.Configuration.IgnoreMissingReadPermissionsTables.IsUnknown() && !r.Configuration.IgnoreMissingReadPermissionsTables.IsNull() {
		*ignoreMissingReadPermissionsTables = r.Configuration.IgnoreMissingReadPermissionsTables.ValueBool()
	} else {
		ignoreMissingReadPermissionsTables = nil
	}
	region := new(shared.SourceDynamodbUpdateDynamodbRegion)
	if !r.Configuration.Region.IsUnknown() && !r.Configuration.Region.IsNull() {
		*region = shared.SourceDynamodbUpdateDynamodbRegion(r.Configuration.Region.ValueString())
	} else {
		region = nil
	}
	reservedAttributeNames := new(string)
	if !r.Configuration.ReservedAttributeNames.IsUnknown() && !r.Configuration.ReservedAttributeNames.IsNull() {
		*reservedAttributeNames = r.Configuration.ReservedAttributeNames.ValueString()
	} else {
		reservedAttributeNames = nil
	}
	configuration := shared.SourceDynamodbUpdate{
		Credentials:                        credentials,
		Endpoint:                           endpoint,
		IgnoreMissingReadPermissionsTables: ignoreMissingReadPermissionsTables,
		Region:                             region,
		ReservedAttributeNames:             reservedAttributeNames,
	}
	out := shared.SourceDynamodbPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
