// 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 *DestinationCobraResourceModel) ToSharedDestinationCobraCreateRequest() *shared.DestinationCobraCreateRequest {
	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 clientID string
	clientID = r.Configuration.ClientID.ValueString()

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

	isSandbox := new(bool)
	if !r.Configuration.IsSandbox.IsUnknown() && !r.Configuration.IsSandbox.IsNull() {
		*isSandbox = r.Configuration.IsSandbox.ValueBool()
	} else {
		isSandbox = nil
	}
	printRecordContentOnError := new(bool)
	if !r.Configuration.PrintRecordContentOnError.IsUnknown() && !r.Configuration.PrintRecordContentOnError.IsNull() {
		*printRecordContentOnError = r.Configuration.PrintRecordContentOnError.ValueBool()
	} else {
		printRecordContentOnError = nil
	}
	var refreshToken string
	refreshToken = r.Configuration.RefreshToken.ValueString()

	var streamMappings []shared.StreamMapping = []shared.StreamMapping{}
	for _, streamMappingsItem := range r.Configuration.StreamMappings {
		var destinationTable string
		destinationTable = streamMappingsItem.DestinationTable.ValueString()

		var sourceStream string
		sourceStream = streamMappingsItem.SourceStream.ValueString()

		updateMode := shared.UpdateMode(streamMappingsItem.UpdateMode.ValueString())
		upsertKey := new(string)
		if !streamMappingsItem.UpsertKey.IsUnknown() && !streamMappingsItem.UpsertKey.IsNull() {
			*upsertKey = streamMappingsItem.UpsertKey.ValueString()
		} else {
			upsertKey = nil
		}
		streamMappings = append(streamMappings, shared.StreamMapping{
			DestinationTable: destinationTable,
			SourceStream:     sourceStream,
			UpdateMode:       updateMode,
			UpsertKey:        upsertKey,
		})
	}
	var streamOrder []string = []string{}
	for _, streamOrderItem := range r.Configuration.StreamOrder {
		streamOrder = append(streamOrder, streamOrderItem.ValueString())
	}
	configuration := shared.DestinationCobra{
		ClientID:                  clientID,
		ClientSecret:              clientSecret,
		IsSandbox:                 isSandbox,
		PrintRecordContentOnError: printRecordContentOnError,
		RefreshToken:              refreshToken,
		StreamMappings:            streamMappings,
		StreamOrder:               streamOrder,
	}
	out := shared.DestinationCobraCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationCobraResourceModel) 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 *DestinationCobraResourceModel) ToSharedDestinationCobraPutRequest() *shared.DestinationCobraPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var clientID string
	clientID = r.Configuration.ClientID.ValueString()

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

	isSandbox := new(bool)
	if !r.Configuration.IsSandbox.IsUnknown() && !r.Configuration.IsSandbox.IsNull() {
		*isSandbox = r.Configuration.IsSandbox.ValueBool()
	} else {
		isSandbox = nil
	}
	printRecordContentOnError := new(bool)
	if !r.Configuration.PrintRecordContentOnError.IsUnknown() && !r.Configuration.PrintRecordContentOnError.IsNull() {
		*printRecordContentOnError = r.Configuration.PrintRecordContentOnError.ValueBool()
	} else {
		printRecordContentOnError = nil
	}
	var refreshToken string
	refreshToken = r.Configuration.RefreshToken.ValueString()

	var streamMappings []shared.DestinationCobraUpdateStreamMapping = []shared.DestinationCobraUpdateStreamMapping{}
	for _, streamMappingsItem := range r.Configuration.StreamMappings {
		var destinationTable string
		destinationTable = streamMappingsItem.DestinationTable.ValueString()

		var sourceStream string
		sourceStream = streamMappingsItem.SourceStream.ValueString()

		updateMode := shared.DestinationCobraUpdateUpdateMode(streamMappingsItem.UpdateMode.ValueString())
		upsertKey := new(string)
		if !streamMappingsItem.UpsertKey.IsUnknown() && !streamMappingsItem.UpsertKey.IsNull() {
			*upsertKey = streamMappingsItem.UpsertKey.ValueString()
		} else {
			upsertKey = nil
		}
		streamMappings = append(streamMappings, shared.DestinationCobraUpdateStreamMapping{
			DestinationTable: destinationTable,
			SourceStream:     sourceStream,
			UpdateMode:       updateMode,
			UpsertKey:        upsertKey,
		})
	}
	var streamOrder []string = []string{}
	for _, streamOrderItem := range r.Configuration.StreamOrder {
		streamOrder = append(streamOrder, streamOrderItem.ValueString())
	}
	configuration := shared.DestinationCobraUpdate{
		ClientID:                  clientID,
		ClientSecret:              clientSecret,
		IsSandbox:                 isSandbox,
		PrintRecordContentOnError: printRecordContentOnError,
		RefreshToken:              refreshToken,
		StreamMappings:            streamMappings,
		StreamOrder:               streamOrder,
	}
	out := shared.DestinationCobraPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
