// 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"
	"time"
)

func (r *SourceConvertkitResourceModel) ToSharedSourceConvertkitCreateRequest() *shared.SourceConvertkitCreateRequest {
	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.AuthenticationType
	var sourceConvertkitOAuth20 *shared.SourceConvertkitOAuth20
	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()

		expiresAt := new(time.Time)
		if !r.Configuration.Credentials.OAuth20.ExpiresAt.IsUnknown() && !r.Configuration.Credentials.OAuth20.ExpiresAt.IsNull() {
			*expiresAt, _ = time.Parse(time.RFC3339Nano, r.Configuration.Credentials.OAuth20.ExpiresAt.ValueString())
		} else {
			expiresAt = nil
		}
		var refreshToken string
		refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()

		sourceConvertkitOAuth20 = &shared.SourceConvertkitOAuth20{
			AccessToken:  accessToken,
			ClientID:     clientID,
			ClientSecret: clientSecret,
			ExpiresAt:    expiresAt,
			RefreshToken: refreshToken,
		}
	}
	if sourceConvertkitOAuth20 != nil {
		credentials = shared.AuthenticationType{
			SourceConvertkitOAuth20: sourceConvertkitOAuth20,
		}
	}
	var apiKey *shared.APIKey
	if r.Configuration.Credentials.APIKey != nil {
		apiKey1 := new(string)
		if !r.Configuration.Credentials.APIKey.APIKey.IsUnknown() && !r.Configuration.Credentials.APIKey.APIKey.IsNull() {
			*apiKey1 = r.Configuration.Credentials.APIKey.APIKey.ValueString()
		} else {
			apiKey1 = nil
		}
		apiKey = &shared.APIKey{
			APIKey: apiKey1,
		}
	}
	if apiKey != nil {
		credentials = shared.AuthenticationType{
			APIKey: apiKey,
		}
	}
	startDate := new(time.Time)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		*startDate, _ = time.Parse(time.RFC3339Nano, r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceConvertkit{
		Credentials: credentials,
		StartDate:   startDate,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceConvertkitCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceConvertkitResourceModel) 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 *SourceConvertkitResourceModel) ToSharedSourceConvertkitPutRequest() *shared.SourceConvertkitPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var credentials shared.SourceConvertkitUpdateAuthenticationType
	var sourceConvertkitUpdateOAuth20 *shared.SourceConvertkitUpdateOAuth20
	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()

		expiresAt := new(time.Time)
		if !r.Configuration.Credentials.OAuth20.ExpiresAt.IsUnknown() && !r.Configuration.Credentials.OAuth20.ExpiresAt.IsNull() {
			*expiresAt, _ = time.Parse(time.RFC3339Nano, r.Configuration.Credentials.OAuth20.ExpiresAt.ValueString())
		} else {
			expiresAt = nil
		}
		var refreshToken string
		refreshToken = r.Configuration.Credentials.OAuth20.RefreshToken.ValueString()

		sourceConvertkitUpdateOAuth20 = &shared.SourceConvertkitUpdateOAuth20{
			AccessToken:  accessToken,
			ClientID:     clientID,
			ClientSecret: clientSecret,
			ExpiresAt:    expiresAt,
			RefreshToken: refreshToken,
		}
	}
	if sourceConvertkitUpdateOAuth20 != nil {
		credentials = shared.SourceConvertkitUpdateAuthenticationType{
			SourceConvertkitUpdateOAuth20: sourceConvertkitUpdateOAuth20,
		}
	}
	var sourceConvertkitUpdateAPIKey *shared.SourceConvertkitUpdateAPIKey
	if r.Configuration.Credentials.APIKey != nil {
		apiKey := new(string)
		if !r.Configuration.Credentials.APIKey.APIKey.IsUnknown() && !r.Configuration.Credentials.APIKey.APIKey.IsNull() {
			*apiKey = r.Configuration.Credentials.APIKey.APIKey.ValueString()
		} else {
			apiKey = nil
		}
		sourceConvertkitUpdateAPIKey = &shared.SourceConvertkitUpdateAPIKey{
			APIKey: apiKey,
		}
	}
	if sourceConvertkitUpdateAPIKey != nil {
		credentials = shared.SourceConvertkitUpdateAuthenticationType{
			SourceConvertkitUpdateAPIKey: sourceConvertkitUpdateAPIKey,
		}
	}
	startDate := new(time.Time)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		*startDate, _ = time.Parse(time.RFC3339Nano, r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceConvertkitUpdate{
		Credentials: credentials,
		StartDate:   startDate,
	}
	out := shared.SourceConvertkitPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
