// 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 *SourcePinterestResourceModel) ToSharedSourcePinterestCreateRequest() *shared.SourcePinterestCreateRequest {
	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()

	accountID := new(string)
	if !r.Configuration.AccountID.IsUnknown() && !r.Configuration.AccountID.IsNull() {
		*accountID = r.Configuration.AccountID.ValueString()
	} else {
		accountID = nil
	}
	var credentials *shared.OAuth20
	if r.Configuration.Credentials != nil {
		var clientID string
		clientID = r.Configuration.Credentials.ClientID.ValueString()

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

		var refreshToken string
		refreshToken = r.Configuration.Credentials.RefreshToken.ValueString()

		credentials = &shared.OAuth20{
			ClientID:     clientID,
			ClientSecret: clientSecret,
			RefreshToken: refreshToken,
		}
	}
	var customReports []shared.ReportConfig = []shared.ReportConfig{}
	for _, customReportsItem := range r.Configuration.CustomReports {
		var attributionTypes []shared.SourcePinterestValidEnums = []shared.SourcePinterestValidEnums{}
		for _, attributionTypesItem := range customReportsItem.AttributionTypes {
			attributionTypes = append(attributionTypes, shared.SourcePinterestValidEnums(attributionTypesItem.ValueString()))
		}
		clickWindowDays := new(shared.ClickWindowDays)
		if !customReportsItem.ClickWindowDays.IsUnknown() && !customReportsItem.ClickWindowDays.IsNull() {
			*clickWindowDays = shared.ClickWindowDays(customReportsItem.ClickWindowDays.ValueInt64())
		} else {
			clickWindowDays = nil
		}
		var columns []shared.SourcePinterestSchemasValidEnums = []shared.SourcePinterestSchemasValidEnums{}
		for _, columnsItem := range customReportsItem.Columns {
			columns = append(columns, shared.SourcePinterestSchemasValidEnums(columnsItem.ValueString()))
		}
		conversionReportTime := new(shared.ConversionReportTime)
		if !customReportsItem.ConversionReportTime.IsUnknown() && !customReportsItem.ConversionReportTime.IsNull() {
			*conversionReportTime = shared.ConversionReportTime(customReportsItem.ConversionReportTime.ValueString())
		} else {
			conversionReportTime = nil
		}
		engagementWindowDays := new(shared.EngagementWindowDays)
		if !customReportsItem.EngagementWindowDays.IsUnknown() && !customReportsItem.EngagementWindowDays.IsNull() {
			*engagementWindowDays = shared.EngagementWindowDays(customReportsItem.EngagementWindowDays.ValueInt64())
		} else {
			engagementWindowDays = nil
		}
		granularity := new(shared.Granularity)
		if !customReportsItem.Granularity.IsUnknown() && !customReportsItem.Granularity.IsNull() {
			*granularity = shared.Granularity(customReportsItem.Granularity.ValueString())
		} else {
			granularity = nil
		}
		level := new(shared.SourcePinterestLevel)
		if !customReportsItem.Level.IsUnknown() && !customReportsItem.Level.IsNull() {
			*level = shared.SourcePinterestLevel(customReportsItem.Level.ValueString())
		} else {
			level = nil
		}
		var name1 string
		name1 = customReportsItem.Name.ValueString()

		startDate := new(customTypes.Date)
		if !customReportsItem.StartDate.IsUnknown() && !customReportsItem.StartDate.IsNull() {
			startDate = customTypes.MustNewDateFromString(customReportsItem.StartDate.ValueString())
		} else {
			startDate = nil
		}
		viewWindowDays := new(shared.ViewWindowDays)
		if !customReportsItem.ViewWindowDays.IsUnknown() && !customReportsItem.ViewWindowDays.IsNull() {
			*viewWindowDays = shared.ViewWindowDays(customReportsItem.ViewWindowDays.ValueInt64())
		} else {
			viewWindowDays = nil
		}
		customReports = append(customReports, shared.ReportConfig{
			AttributionTypes:     attributionTypes,
			ClickWindowDays:      clickWindowDays,
			Columns:              columns,
			ConversionReportTime: conversionReportTime,
			EngagementWindowDays: engagementWindowDays,
			Granularity:          granularity,
			Level:                level,
			Name:                 name1,
			StartDate:            startDate,
			ViewWindowDays:       viewWindowDays,
		})
	}
	startDate1 := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate1 = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate1 = nil
	}
	var status []shared.Status = []shared.Status{}
	for _, statusItem := range r.Configuration.Status {
		status = append(status, shared.Status(statusItem.ValueString()))
	}
	configuration := shared.SourcePinterest{
		AccountID:     accountID,
		Credentials:   credentials,
		CustomReports: customReports,
		StartDate:     startDate1,
		Status:        status,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourcePinterestCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourcePinterestResourceModel) 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 *SourcePinterestResourceModel) ToSharedSourcePinterestPutRequest() *shared.SourcePinterestPutRequest {
	var name string
	name = r.Name.ValueString()

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

	accountID := new(string)
	if !r.Configuration.AccountID.IsUnknown() && !r.Configuration.AccountID.IsNull() {
		*accountID = r.Configuration.AccountID.ValueString()
	} else {
		accountID = nil
	}
	var credentials *shared.SourcePinterestUpdateOAuth20
	if r.Configuration.Credentials != nil {
		var clientID string
		clientID = r.Configuration.Credentials.ClientID.ValueString()

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

		var refreshToken string
		refreshToken = r.Configuration.Credentials.RefreshToken.ValueString()

		credentials = &shared.SourcePinterestUpdateOAuth20{
			ClientID:     clientID,
			ClientSecret: clientSecret,
			RefreshToken: refreshToken,
		}
	}
	var customReports []shared.SourcePinterestUpdateReportConfig = []shared.SourcePinterestUpdateReportConfig{}
	for _, customReportsItem := range r.Configuration.CustomReports {
		var attributionTypes []shared.SourcePinterestUpdateValidEnums = []shared.SourcePinterestUpdateValidEnums{}
		for _, attributionTypesItem := range customReportsItem.AttributionTypes {
			attributionTypes = append(attributionTypes, shared.SourcePinterestUpdateValidEnums(attributionTypesItem.ValueString()))
		}
		clickWindowDays := new(shared.SourcePinterestUpdateClickWindowDays)
		if !customReportsItem.ClickWindowDays.IsUnknown() && !customReportsItem.ClickWindowDays.IsNull() {
			*clickWindowDays = shared.SourcePinterestUpdateClickWindowDays(customReportsItem.ClickWindowDays.ValueInt64())
		} else {
			clickWindowDays = nil
		}
		var columns []shared.SourcePinterestUpdateSchemasValidEnums = []shared.SourcePinterestUpdateSchemasValidEnums{}
		for _, columnsItem := range customReportsItem.Columns {
			columns = append(columns, shared.SourcePinterestUpdateSchemasValidEnums(columnsItem.ValueString()))
		}
		conversionReportTime := new(shared.SourcePinterestUpdateConversionReportTime)
		if !customReportsItem.ConversionReportTime.IsUnknown() && !customReportsItem.ConversionReportTime.IsNull() {
			*conversionReportTime = shared.SourcePinterestUpdateConversionReportTime(customReportsItem.ConversionReportTime.ValueString())
		} else {
			conversionReportTime = nil
		}
		engagementWindowDays := new(shared.SourcePinterestUpdateEngagementWindowDays)
		if !customReportsItem.EngagementWindowDays.IsUnknown() && !customReportsItem.EngagementWindowDays.IsNull() {
			*engagementWindowDays = shared.SourcePinterestUpdateEngagementWindowDays(customReportsItem.EngagementWindowDays.ValueInt64())
		} else {
			engagementWindowDays = nil
		}
		granularity := new(shared.SourcePinterestUpdateGranularity)
		if !customReportsItem.Granularity.IsUnknown() && !customReportsItem.Granularity.IsNull() {
			*granularity = shared.SourcePinterestUpdateGranularity(customReportsItem.Granularity.ValueString())
		} else {
			granularity = nil
		}
		level := new(shared.SourcePinterestUpdateLevel)
		if !customReportsItem.Level.IsUnknown() && !customReportsItem.Level.IsNull() {
			*level = shared.SourcePinterestUpdateLevel(customReportsItem.Level.ValueString())
		} else {
			level = nil
		}
		var name1 string
		name1 = customReportsItem.Name.ValueString()

		startDate := new(customTypes.Date)
		if !customReportsItem.StartDate.IsUnknown() && !customReportsItem.StartDate.IsNull() {
			startDate = customTypes.MustNewDateFromString(customReportsItem.StartDate.ValueString())
		} else {
			startDate = nil
		}
		viewWindowDays := new(shared.SourcePinterestUpdateViewWindowDays)
		if !customReportsItem.ViewWindowDays.IsUnknown() && !customReportsItem.ViewWindowDays.IsNull() {
			*viewWindowDays = shared.SourcePinterestUpdateViewWindowDays(customReportsItem.ViewWindowDays.ValueInt64())
		} else {
			viewWindowDays = nil
		}
		customReports = append(customReports, shared.SourcePinterestUpdateReportConfig{
			AttributionTypes:     attributionTypes,
			ClickWindowDays:      clickWindowDays,
			Columns:              columns,
			ConversionReportTime: conversionReportTime,
			EngagementWindowDays: engagementWindowDays,
			Granularity:          granularity,
			Level:                level,
			Name:                 name1,
			StartDate:            startDate,
			ViewWindowDays:       viewWindowDays,
		})
	}
	startDate1 := new(customTypes.Date)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		startDate1 = customTypes.MustNewDateFromString(r.Configuration.StartDate.ValueString())
	} else {
		startDate1 = nil
	}
	var status []shared.SourcePinterestUpdateStatus = []shared.SourcePinterestUpdateStatus{}
	for _, statusItem := range r.Configuration.Status {
		status = append(status, shared.SourcePinterestUpdateStatus(statusItem.ValueString()))
	}
	configuration := shared.SourcePinterestUpdate{
		AccountID:     accountID,
		Credentials:   credentials,
		CustomReports: customReports,
		StartDate:     startDate1,
		Status:        status,
	}
	out := shared.SourcePinterestPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
