// 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 *SourceBingAdsResourceModel) ToSharedSourceBingAdsCreateRequest() *shared.SourceBingAdsCreateRequest {
	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 accountNames []shared.AccountNames = []shared.AccountNames{}
	for _, accountNamesItem := range r.Configuration.AccountNames {
		var name1 string
		name1 = accountNamesItem.Name.ValueString()

		operator := shared.Operator(accountNamesItem.Operator.ValueString())
		accountNames = append(accountNames, shared.AccountNames{
			Name:     name1,
			Operator: operator,
		})
	}
	var clientID string
	clientID = r.Configuration.ClientID.ValueString()

	clientSecret := new(string)
	if !r.Configuration.ClientSecret.IsUnknown() && !r.Configuration.ClientSecret.IsNull() {
		*clientSecret = r.Configuration.ClientSecret.ValueString()
	} else {
		clientSecret = nil
	}
	var customReports []shared.CustomReportConfig = []shared.CustomReportConfig{}
	for _, customReportsItem := range r.Configuration.CustomReports {
		var name2 string
		name2 = customReportsItem.Name.ValueString()

		reportAggregation := new(string)
		if !customReportsItem.ReportAggregation.IsUnknown() && !customReportsItem.ReportAggregation.IsNull() {
			*reportAggregation = customReportsItem.ReportAggregation.ValueString()
		} else {
			reportAggregation = nil
		}
		var reportColumns []string = []string{}
		for _, reportColumnsItem := range customReportsItem.ReportColumns {
			reportColumns = append(reportColumns, reportColumnsItem.ValueString())
		}
		reportingObject := shared.ReportingDataObject(customReportsItem.ReportingObject.ValueString())
		customReports = append(customReports, shared.CustomReportConfig{
			Name:              name2,
			ReportAggregation: reportAggregation,
			ReportColumns:     reportColumns,
			ReportingObject:   reportingObject,
		})
	}
	var developerToken string
	developerToken = r.Configuration.DeveloperToken.ValueString()

	lookbackWindow := new(int64)
	if !r.Configuration.LookbackWindow.IsUnknown() && !r.Configuration.LookbackWindow.IsNull() {
		*lookbackWindow = r.Configuration.LookbackWindow.ValueInt64()
	} else {
		lookbackWindow = nil
	}
	var refreshToken string
	refreshToken = r.Configuration.RefreshToken.ValueString()

	reportsStartDate := new(customTypes.Date)
	if !r.Configuration.ReportsStartDate.IsUnknown() && !r.Configuration.ReportsStartDate.IsNull() {
		reportsStartDate = customTypes.MustNewDateFromString(r.Configuration.ReportsStartDate.ValueString())
	} else {
		reportsStartDate = nil
	}
	tenantID := new(string)
	if !r.Configuration.TenantID.IsUnknown() && !r.Configuration.TenantID.IsNull() {
		*tenantID = r.Configuration.TenantID.ValueString()
	} else {
		tenantID = nil
	}
	configuration := shared.SourceBingAds{
		AccountNames:     accountNames,
		ClientID:         clientID,
		ClientSecret:     clientSecret,
		CustomReports:    customReports,
		DeveloperToken:   developerToken,
		LookbackWindow:   lookbackWindow,
		RefreshToken:     refreshToken,
		ReportsStartDate: reportsStartDate,
		TenantID:         tenantID,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceBingAdsCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceBingAdsResourceModel) 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 *SourceBingAdsResourceModel) ToSharedSourceBingAdsPutRequest() *shared.SourceBingAdsPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var accountNames []shared.SourceBingAdsUpdateAccountNames = []shared.SourceBingAdsUpdateAccountNames{}
	for _, accountNamesItem := range r.Configuration.AccountNames {
		var name1 string
		name1 = accountNamesItem.Name.ValueString()

		operator := shared.SourceBingAdsUpdateOperator(accountNamesItem.Operator.ValueString())
		accountNames = append(accountNames, shared.SourceBingAdsUpdateAccountNames{
			Name:     name1,
			Operator: operator,
		})
	}
	var clientID string
	clientID = r.Configuration.ClientID.ValueString()

	clientSecret := new(string)
	if !r.Configuration.ClientSecret.IsUnknown() && !r.Configuration.ClientSecret.IsNull() {
		*clientSecret = r.Configuration.ClientSecret.ValueString()
	} else {
		clientSecret = nil
	}
	var customReports []shared.SourceBingAdsUpdateCustomReportConfig = []shared.SourceBingAdsUpdateCustomReportConfig{}
	for _, customReportsItem := range r.Configuration.CustomReports {
		var name2 string
		name2 = customReportsItem.Name.ValueString()

		reportAggregation := new(string)
		if !customReportsItem.ReportAggregation.IsUnknown() && !customReportsItem.ReportAggregation.IsNull() {
			*reportAggregation = customReportsItem.ReportAggregation.ValueString()
		} else {
			reportAggregation = nil
		}
		var reportColumns []string = []string{}
		for _, reportColumnsItem := range customReportsItem.ReportColumns {
			reportColumns = append(reportColumns, reportColumnsItem.ValueString())
		}
		reportingObject := shared.SourceBingAdsUpdateReportingDataObject(customReportsItem.ReportingObject.ValueString())
		customReports = append(customReports, shared.SourceBingAdsUpdateCustomReportConfig{
			Name:              name2,
			ReportAggregation: reportAggregation,
			ReportColumns:     reportColumns,
			ReportingObject:   reportingObject,
		})
	}
	var developerToken string
	developerToken = r.Configuration.DeveloperToken.ValueString()

	lookbackWindow := new(int64)
	if !r.Configuration.LookbackWindow.IsUnknown() && !r.Configuration.LookbackWindow.IsNull() {
		*lookbackWindow = r.Configuration.LookbackWindow.ValueInt64()
	} else {
		lookbackWindow = nil
	}
	var refreshToken string
	refreshToken = r.Configuration.RefreshToken.ValueString()

	reportsStartDate := new(customTypes.Date)
	if !r.Configuration.ReportsStartDate.IsUnknown() && !r.Configuration.ReportsStartDate.IsNull() {
		reportsStartDate = customTypes.MustNewDateFromString(r.Configuration.ReportsStartDate.ValueString())
	} else {
		reportsStartDate = nil
	}
	tenantID := new(string)
	if !r.Configuration.TenantID.IsUnknown() && !r.Configuration.TenantID.IsNull() {
		*tenantID = r.Configuration.TenantID.ValueString()
	} else {
		tenantID = nil
	}
	configuration := shared.SourceBingAdsUpdate{
		AccountNames:     accountNames,
		ClientID:         clientID,
		ClientSecret:     clientSecret,
		CustomReports:    customReports,
		DeveloperToken:   developerToken,
		LookbackWindow:   lookbackWindow,
		RefreshToken:     refreshToken,
		ReportsStartDate: reportsStartDate,
		TenantID:         tenantID,
	}
	out := shared.SourceBingAdsPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
