// 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"
	customTypes "github.com/airbytehq/terraform-provider-airbyte/internal/sdk/types"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *SourceTiktokMarketingResourceModel) ToSharedSourceTiktokMarketingCreateRequest() *shared.SourceTiktokMarketingCreateRequest {
	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()

	attributionWindow := new(int64)
	if !r.Configuration.AttributionWindow.IsUnknown() && !r.Configuration.AttributionWindow.IsNull() {
		*attributionWindow = r.Configuration.AttributionWindow.ValueInt64()
	} else {
		attributionWindow = nil
	}
	var credentials *shared.SourceTiktokMarketingAuthenticationMethod
	if r.Configuration.Credentials != nil {
		var sourceTiktokMarketingOAuth20 *shared.SourceTiktokMarketingOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			var accessToken string
			accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()

			advertiserID := new(string)
			if !r.Configuration.Credentials.OAuth20.AdvertiserID.IsUnknown() && !r.Configuration.Credentials.OAuth20.AdvertiserID.IsNull() {
				*advertiserID = r.Configuration.Credentials.OAuth20.AdvertiserID.ValueString()
			} else {
				advertiserID = nil
			}
			var appID string
			appID = r.Configuration.Credentials.OAuth20.AppID.ValueString()

			var secret string
			secret = r.Configuration.Credentials.OAuth20.Secret.ValueString()

			sourceTiktokMarketingOAuth20 = &shared.SourceTiktokMarketingOAuth20{
				AccessToken:  accessToken,
				AdvertiserID: advertiserID,
				AppID:        appID,
				Secret:       secret,
			}
		}
		if sourceTiktokMarketingOAuth20 != nil {
			credentials = &shared.SourceTiktokMarketingAuthenticationMethod{
				SourceTiktokMarketingOAuth20: sourceTiktokMarketingOAuth20,
			}
		}
		var sandboxAccessToken *shared.SandboxAccessToken
		if r.Configuration.Credentials.SandboxAccessToken != nil {
			var accessToken1 string
			accessToken1 = r.Configuration.Credentials.SandboxAccessToken.AccessToken.ValueString()

			var advertiserId1 string
			advertiserId1 = r.Configuration.Credentials.SandboxAccessToken.AdvertiserID.ValueString()

			sandboxAccessToken = &shared.SandboxAccessToken{
				AccessToken:  accessToken1,
				AdvertiserID: advertiserId1,
			}
		}
		if sandboxAccessToken != nil {
			credentials = &shared.SourceTiktokMarketingAuthenticationMethod{
				SandboxAccessToken: sandboxAccessToken,
			}
		}
	}
	endDate := new(customTypes.Date)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		endDate = customTypes.MustNewDateFromString(r.Configuration.EndDate.ValueString())
	} else {
		endDate = nil
	}
	includeDeleted := new(bool)
	if !r.Configuration.IncludeDeleted.IsUnknown() && !r.Configuration.IncludeDeleted.IsNull() {
		*includeDeleted = r.Configuration.IncludeDeleted.ValueBool()
	} else {
		includeDeleted = nil
	}
	startDate := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceTiktokMarketing{
		AttributionWindow: attributionWindow,
		Credentials:       credentials,
		EndDate:           endDate,
		IncludeDeleted:    includeDeleted,
		StartDate:         startDate,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceTiktokMarketingCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceTiktokMarketingResourceModel) 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 *SourceTiktokMarketingResourceModel) ToSharedSourceTiktokMarketingPutRequest() *shared.SourceTiktokMarketingPutRequest {
	var name string
	name = r.Name.ValueString()

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

	attributionWindow := new(int64)
	if !r.Configuration.AttributionWindow.IsUnknown() && !r.Configuration.AttributionWindow.IsNull() {
		*attributionWindow = r.Configuration.AttributionWindow.ValueInt64()
	} else {
		attributionWindow = nil
	}
	var credentials *shared.SourceTiktokMarketingUpdateAuthenticationMethod
	if r.Configuration.Credentials != nil {
		var sourceTiktokMarketingUpdateOAuth20 *shared.SourceTiktokMarketingUpdateOAuth20
		if r.Configuration.Credentials.OAuth20 != nil {
			var accessToken string
			accessToken = r.Configuration.Credentials.OAuth20.AccessToken.ValueString()

			advertiserID := new(string)
			if !r.Configuration.Credentials.OAuth20.AdvertiserID.IsUnknown() && !r.Configuration.Credentials.OAuth20.AdvertiserID.IsNull() {
				*advertiserID = r.Configuration.Credentials.OAuth20.AdvertiserID.ValueString()
			} else {
				advertiserID = nil
			}
			var appID string
			appID = r.Configuration.Credentials.OAuth20.AppID.ValueString()

			var secret string
			secret = r.Configuration.Credentials.OAuth20.Secret.ValueString()

			sourceTiktokMarketingUpdateOAuth20 = &shared.SourceTiktokMarketingUpdateOAuth20{
				AccessToken:  accessToken,
				AdvertiserID: advertiserID,
				AppID:        appID,
				Secret:       secret,
			}
		}
		if sourceTiktokMarketingUpdateOAuth20 != nil {
			credentials = &shared.SourceTiktokMarketingUpdateAuthenticationMethod{
				SourceTiktokMarketingUpdateOAuth20: sourceTiktokMarketingUpdateOAuth20,
			}
		}
		var sourceTiktokMarketingUpdateSandboxAccessToken *shared.SourceTiktokMarketingUpdateSandboxAccessToken
		if r.Configuration.Credentials.SandboxAccessToken != nil {
			var accessToken1 string
			accessToken1 = r.Configuration.Credentials.SandboxAccessToken.AccessToken.ValueString()

			var advertiserId1 string
			advertiserId1 = r.Configuration.Credentials.SandboxAccessToken.AdvertiserID.ValueString()

			sourceTiktokMarketingUpdateSandboxAccessToken = &shared.SourceTiktokMarketingUpdateSandboxAccessToken{
				AccessToken:  accessToken1,
				AdvertiserID: advertiserId1,
			}
		}
		if sourceTiktokMarketingUpdateSandboxAccessToken != nil {
			credentials = &shared.SourceTiktokMarketingUpdateAuthenticationMethod{
				SourceTiktokMarketingUpdateSandboxAccessToken: sourceTiktokMarketingUpdateSandboxAccessToken,
			}
		}
	}
	endDate := new(customTypes.Date)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		endDate = customTypes.MustNewDateFromString(r.Configuration.EndDate.ValueString())
	} else {
		endDate = nil
	}
	includeDeleted := new(bool)
	if !r.Configuration.IncludeDeleted.IsUnknown() && !r.Configuration.IncludeDeleted.IsNull() {
		*includeDeleted = r.Configuration.IncludeDeleted.ValueBool()
	} else {
		includeDeleted = nil
	}
	startDate := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate = nil
	}
	configuration := shared.SourceTiktokMarketingUpdate{
		AttributionWindow: attributionWindow,
		Credentials:       credentials,
		EndDate:           endDate,
		IncludeDeleted:    includeDeleted,
		StartDate:         startDate,
	}
	out := shared.SourceTiktokMarketingPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
