// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package shared

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/internal/utils"
	"time"
)

type SourceConvertkitUpdateSchemasAuthType string

const (
	SourceConvertkitUpdateSchemasAuthTypeAPIKey SourceConvertkitUpdateSchemasAuthType = "api_key"
)

func (e SourceConvertkitUpdateSchemasAuthType) ToPointer() *SourceConvertkitUpdateSchemasAuthType {
	return &e
}
func (e *SourceConvertkitUpdateSchemasAuthType) UnmarshalJSON(data []byte) error {
	var v string
	if err := json.Unmarshal(data, &v); err != nil {
		return err
	}
	switch v {
	case "api_key":
		*e = SourceConvertkitUpdateSchemasAuthType(v)
		return nil
	default:
		return fmt.Errorf("invalid value for SourceConvertkitUpdateSchemasAuthType: %v", v)
	}
}

type SourceConvertkitUpdateAPIKey struct {
	// Kit/ConvertKit API Key
	APIKey   *string                               `default:"{{ config.get('credentials',{}).get('api_key') or config.get('api_secret') }}" json:"api_key"`
	authType SourceConvertkitUpdateSchemasAuthType `const:"api_key" json:"auth_type"`
}

func (s SourceConvertkitUpdateAPIKey) MarshalJSON() ([]byte, error) {
	return utils.MarshalJSON(s, "", false)
}

func (s *SourceConvertkitUpdateAPIKey) UnmarshalJSON(data []byte) error {
	if err := utils.UnmarshalJSON(data, &s, "", false, true); err != nil {
		return err
	}
	return nil
}

func (o *SourceConvertkitUpdateAPIKey) GetAPIKey() *string {
	if o == nil {
		return nil
	}
	return o.APIKey
}

func (o *SourceConvertkitUpdateAPIKey) GetAuthType() SourceConvertkitUpdateSchemasAuthType {
	return SourceConvertkitUpdateSchemasAuthTypeAPIKey
}

type SourceConvertkitUpdateAuthType string

const (
	SourceConvertkitUpdateAuthTypeOauth20 SourceConvertkitUpdateAuthType = "oauth2.0"
)

func (e SourceConvertkitUpdateAuthType) ToPointer() *SourceConvertkitUpdateAuthType {
	return &e
}
func (e *SourceConvertkitUpdateAuthType) UnmarshalJSON(data []byte) error {
	var v string
	if err := json.Unmarshal(data, &v); err != nil {
		return err
	}
	switch v {
	case "oauth2.0":
		*e = SourceConvertkitUpdateAuthType(v)
		return nil
	default:
		return fmt.Errorf("invalid value for SourceConvertkitUpdateAuthType: %v", v)
	}
}

type SourceConvertkitUpdateOAuth20 struct {
	// An access token generated using the provided client information and refresh token.
	AccessToken *string                        `json:"access_token,omitempty"`
	authType    SourceConvertkitUpdateAuthType `const:"oauth2.0" json:"auth_type"`
	// The client ID of your OAuth application.
	ClientID string `json:"client_id"`
	// The client secret of your OAuth application.
	ClientSecret string `json:"client_secret"`
	// The time at which the current access token is set to expire
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
	// A current, non-expired refresh token genereted using the provided client ID and secret.
	RefreshToken string `json:"refresh_token"`
}

func (s SourceConvertkitUpdateOAuth20) MarshalJSON() ([]byte, error) {
	return utils.MarshalJSON(s, "", false)
}

func (s *SourceConvertkitUpdateOAuth20) UnmarshalJSON(data []byte) error {
	if err := utils.UnmarshalJSON(data, &s, "", false, true); err != nil {
		return err
	}
	return nil
}

func (o *SourceConvertkitUpdateOAuth20) GetAccessToken() *string {
	if o == nil {
		return nil
	}
	return o.AccessToken
}

func (o *SourceConvertkitUpdateOAuth20) GetAuthType() SourceConvertkitUpdateAuthType {
	return SourceConvertkitUpdateAuthTypeOauth20
}

func (o *SourceConvertkitUpdateOAuth20) GetClientID() string {
	if o == nil {
		return ""
	}
	return o.ClientID
}

func (o *SourceConvertkitUpdateOAuth20) GetClientSecret() string {
	if o == nil {
		return ""
	}
	return o.ClientSecret
}

func (o *SourceConvertkitUpdateOAuth20) GetExpiresAt() *time.Time {
	if o == nil {
		return nil
	}
	return o.ExpiresAt
}

func (o *SourceConvertkitUpdateOAuth20) GetRefreshToken() string {
	if o == nil {
		return ""
	}
	return o.RefreshToken
}

type SourceConvertkitUpdateAuthenticationTypeType string

const (
	SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateOAuth20 SourceConvertkitUpdateAuthenticationTypeType = "source-convertkit-update_OAuth 2.0"
	SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateAPIKey  SourceConvertkitUpdateAuthenticationTypeType = "source-convertkit-update_API Key"
)

type SourceConvertkitUpdateAuthenticationType struct {
	SourceConvertkitUpdateOAuth20 *SourceConvertkitUpdateOAuth20 `queryParam:"inline"`
	SourceConvertkitUpdateAPIKey  *SourceConvertkitUpdateAPIKey  `queryParam:"inline"`

	Type SourceConvertkitUpdateAuthenticationTypeType
}

func CreateSourceConvertkitUpdateAuthenticationTypeSourceConvertkitUpdateOAuth20(sourceConvertkitUpdateOAuth20 SourceConvertkitUpdateOAuth20) SourceConvertkitUpdateAuthenticationType {
	typ := SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateOAuth20

	return SourceConvertkitUpdateAuthenticationType{
		SourceConvertkitUpdateOAuth20: &sourceConvertkitUpdateOAuth20,
		Type:                          typ,
	}
}

func CreateSourceConvertkitUpdateAuthenticationTypeSourceConvertkitUpdateAPIKey(sourceConvertkitUpdateAPIKey SourceConvertkitUpdateAPIKey) SourceConvertkitUpdateAuthenticationType {
	typ := SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateAPIKey

	return SourceConvertkitUpdateAuthenticationType{
		SourceConvertkitUpdateAPIKey: &sourceConvertkitUpdateAPIKey,
		Type:                         typ,
	}
}

func (u *SourceConvertkitUpdateAuthenticationType) UnmarshalJSON(data []byte) error {

	var sourceConvertkitUpdateAPIKey SourceConvertkitUpdateAPIKey = SourceConvertkitUpdateAPIKey{}
	if err := utils.UnmarshalJSON(data, &sourceConvertkitUpdateAPIKey, "", true, true); err == nil {
		u.SourceConvertkitUpdateAPIKey = &sourceConvertkitUpdateAPIKey
		u.Type = SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateAPIKey
		return nil
	}

	var sourceConvertkitUpdateOAuth20 SourceConvertkitUpdateOAuth20 = SourceConvertkitUpdateOAuth20{}
	if err := utils.UnmarshalJSON(data, &sourceConvertkitUpdateOAuth20, "", true, true); err == nil {
		u.SourceConvertkitUpdateOAuth20 = &sourceConvertkitUpdateOAuth20
		u.Type = SourceConvertkitUpdateAuthenticationTypeTypeSourceConvertkitUpdateOAuth20
		return nil
	}

	return fmt.Errorf("could not unmarshal `%s` into any supported union types for SourceConvertkitUpdateAuthenticationType", string(data))
}

func (u SourceConvertkitUpdateAuthenticationType) MarshalJSON() ([]byte, error) {
	if u.SourceConvertkitUpdateOAuth20 != nil {
		return utils.MarshalJSON(u.SourceConvertkitUpdateOAuth20, "", true)
	}

	if u.SourceConvertkitUpdateAPIKey != nil {
		return utils.MarshalJSON(u.SourceConvertkitUpdateAPIKey, "", true)
	}

	return nil, errors.New("could not marshal union type SourceConvertkitUpdateAuthenticationType: all fields are null")
}

type SourceConvertkitUpdate struct {
	Credentials SourceConvertkitUpdateAuthenticationType `json:"credentials"`
	StartDate   *time.Time                               `default:"2013-01-01T00:00:00Z" json:"start_date"`
}

func (s SourceConvertkitUpdate) MarshalJSON() ([]byte, error) {
	return utils.MarshalJSON(s, "", false)
}

func (s *SourceConvertkitUpdate) UnmarshalJSON(data []byte) error {
	if err := utils.UnmarshalJSON(data, &s, "", false, false); err != nil {
		return err
	}
	return nil
}

func (o *SourceConvertkitUpdate) GetCredentials() SourceConvertkitUpdateAuthenticationType {
	if o == nil {
		return SourceConvertkitUpdateAuthenticationType{}
	}
	return o.Credentials
}

func (o *SourceConvertkitUpdate) GetStartDate() *time.Time {
	if o == nil {
		return nil
	}
	return o.StartDate
}
