// 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 *SourceMixpanelResourceModel) ToSharedSourceMixpanelCreateRequest() *shared.SourceMixpanelCreateRequest {
	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.AuthenticationWildcard
	var serviceAccount *shared.ServiceAccount
	if r.Configuration.Credentials.ServiceAccount != nil {
		var projectID int64
		projectID = r.Configuration.Credentials.ServiceAccount.ProjectID.ValueInt64()

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

		var username string
		username = r.Configuration.Credentials.ServiceAccount.Username.ValueString()

		serviceAccount = &shared.ServiceAccount{
			ProjectID: projectID,
			Secret:    secret,
			Username:  username,
		}
	}
	if serviceAccount != nil {
		credentials = shared.AuthenticationWildcard{
			ServiceAccount: serviceAccount,
		}
	}
	var projectSecret *shared.ProjectSecret
	if r.Configuration.Credentials.ProjectSecret != nil {
		var apiSecret string
		apiSecret = r.Configuration.Credentials.ProjectSecret.APISecret.ValueString()

		projectSecret = &shared.ProjectSecret{
			APISecret: apiSecret,
		}
	}
	if projectSecret != nil {
		credentials = shared.AuthenticationWildcard{
			ProjectSecret: projectSecret,
		}
	}
	dateWindowSize := new(int64)
	if !r.Configuration.DateWindowSize.IsUnknown() && !r.Configuration.DateWindowSize.IsNull() {
		*dateWindowSize = r.Configuration.DateWindowSize.ValueInt64()
	} else {
		dateWindowSize = nil
	}
	endDate := new(time.Time)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		*endDate, _ = time.Parse(time.RFC3339Nano, r.Configuration.EndDate.ValueString())
	} else {
		endDate = nil
	}
	exportLookbackWindow := new(int64)
	if !r.Configuration.ExportLookbackWindow.IsUnknown() && !r.Configuration.ExportLookbackWindow.IsNull() {
		*exportLookbackWindow = r.Configuration.ExportLookbackWindow.ValueInt64()
	} else {
		exportLookbackWindow = nil
	}
	pageSize := new(int64)
	if !r.Configuration.PageSize.IsUnknown() && !r.Configuration.PageSize.IsNull() {
		*pageSize = r.Configuration.PageSize.ValueInt64()
	} else {
		pageSize = nil
	}
	projectTimezone := new(string)
	if !r.Configuration.ProjectTimezone.IsUnknown() && !r.Configuration.ProjectTimezone.IsNull() {
		*projectTimezone = r.Configuration.ProjectTimezone.ValueString()
	} else {
		projectTimezone = nil
	}
	region := new(shared.SourceMixpanelRegion)
	if !r.Configuration.Region.IsUnknown() && !r.Configuration.Region.IsNull() {
		*region = shared.SourceMixpanelRegion(r.Configuration.Region.ValueString())
	} else {
		region = nil
	}
	selectPropertiesByDefault := new(bool)
	if !r.Configuration.SelectPropertiesByDefault.IsUnknown() && !r.Configuration.SelectPropertiesByDefault.IsNull() {
		*selectPropertiesByDefault = r.Configuration.SelectPropertiesByDefault.ValueBool()
	} else {
		selectPropertiesByDefault = nil
	}
	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.SourceMixpanel{
		AttributionWindow:         attributionWindow,
		Credentials:               credentials,
		DateWindowSize:            dateWindowSize,
		EndDate:                   endDate,
		ExportLookbackWindow:      exportLookbackWindow,
		PageSize:                  pageSize,
		ProjectTimezone:           projectTimezone,
		Region:                    region,
		SelectPropertiesByDefault: selectPropertiesByDefault,
		StartDate:                 startDate,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceMixpanelCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceMixpanelResourceModel) 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 *SourceMixpanelResourceModel) ToSharedSourceMixpanelPutRequest() *shared.SourceMixpanelPutRequest {
	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.SourceMixpanelUpdateAuthenticationWildcard
	var sourceMixpanelUpdateServiceAccount *shared.SourceMixpanelUpdateServiceAccount
	if r.Configuration.Credentials.ServiceAccount != nil {
		var projectID int64
		projectID = r.Configuration.Credentials.ServiceAccount.ProjectID.ValueInt64()

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

		var username string
		username = r.Configuration.Credentials.ServiceAccount.Username.ValueString()

		sourceMixpanelUpdateServiceAccount = &shared.SourceMixpanelUpdateServiceAccount{
			ProjectID: projectID,
			Secret:    secret,
			Username:  username,
		}
	}
	if sourceMixpanelUpdateServiceAccount != nil {
		credentials = shared.SourceMixpanelUpdateAuthenticationWildcard{
			SourceMixpanelUpdateServiceAccount: sourceMixpanelUpdateServiceAccount,
		}
	}
	var sourceMixpanelUpdateProjectSecret *shared.SourceMixpanelUpdateProjectSecret
	if r.Configuration.Credentials.ProjectSecret != nil {
		var apiSecret string
		apiSecret = r.Configuration.Credentials.ProjectSecret.APISecret.ValueString()

		sourceMixpanelUpdateProjectSecret = &shared.SourceMixpanelUpdateProjectSecret{
			APISecret: apiSecret,
		}
	}
	if sourceMixpanelUpdateProjectSecret != nil {
		credentials = shared.SourceMixpanelUpdateAuthenticationWildcard{
			SourceMixpanelUpdateProjectSecret: sourceMixpanelUpdateProjectSecret,
		}
	}
	dateWindowSize := new(int64)
	if !r.Configuration.DateWindowSize.IsUnknown() && !r.Configuration.DateWindowSize.IsNull() {
		*dateWindowSize = r.Configuration.DateWindowSize.ValueInt64()
	} else {
		dateWindowSize = nil
	}
	endDate := new(time.Time)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		*endDate, _ = time.Parse(time.RFC3339Nano, r.Configuration.EndDate.ValueString())
	} else {
		endDate = nil
	}
	exportLookbackWindow := new(int64)
	if !r.Configuration.ExportLookbackWindow.IsUnknown() && !r.Configuration.ExportLookbackWindow.IsNull() {
		*exportLookbackWindow = r.Configuration.ExportLookbackWindow.ValueInt64()
	} else {
		exportLookbackWindow = nil
	}
	pageSize := new(int64)
	if !r.Configuration.PageSize.IsUnknown() && !r.Configuration.PageSize.IsNull() {
		*pageSize = r.Configuration.PageSize.ValueInt64()
	} else {
		pageSize = nil
	}
	projectTimezone := new(string)
	if !r.Configuration.ProjectTimezone.IsUnknown() && !r.Configuration.ProjectTimezone.IsNull() {
		*projectTimezone = r.Configuration.ProjectTimezone.ValueString()
	} else {
		projectTimezone = nil
	}
	region := new(shared.SourceMixpanelUpdateRegion)
	if !r.Configuration.Region.IsUnknown() && !r.Configuration.Region.IsNull() {
		*region = shared.SourceMixpanelUpdateRegion(r.Configuration.Region.ValueString())
	} else {
		region = nil
	}
	selectPropertiesByDefault := new(bool)
	if !r.Configuration.SelectPropertiesByDefault.IsUnknown() && !r.Configuration.SelectPropertiesByDefault.IsNull() {
		*selectPropertiesByDefault = r.Configuration.SelectPropertiesByDefault.ValueBool()
	} else {
		selectPropertiesByDefault = nil
	}
	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.SourceMixpanelUpdate{
		AttributionWindow:         attributionWindow,
		Credentials:               credentials,
		DateWindowSize:            dateWindowSize,
		EndDate:                   endDate,
		ExportLookbackWindow:      exportLookbackWindow,
		PageSize:                  pageSize,
		ProjectTimezone:           projectTimezone,
		Region:                    region,
		SelectPropertiesByDefault: selectPropertiesByDefault,
		StartDate:                 startDate,
	}
	out := shared.SourceMixpanelPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
