// 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 SourceAirtableUpdateSchemasAuthMethod string

const (
	SourceAirtableUpdateSchemasAuthMethodAPIKey SourceAirtableUpdateSchemasAuthMethod = "api_key"
)

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

type SourceAirtableUpdatePersonalAccessToken struct {
	// The Personal Access Token for the Airtable account. See the <a href="https://airtable.com/developers/web/guides/personal-access-tokens">Support Guide</a> for more information on how to obtain this token.
	APIKey     string                                 `json:"api_key"`
	authMethod *SourceAirtableUpdateSchemasAuthMethod `const:"api_key" json:"auth_method,omitempty"`
}

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

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

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

func (o *SourceAirtableUpdatePersonalAccessToken) GetAuthMethod() *SourceAirtableUpdateSchemasAuthMethod {
	return SourceAirtableUpdateSchemasAuthMethodAPIKey.ToPointer()
}

type SourceAirtableUpdateAuthMethod string

const (
	SourceAirtableUpdateAuthMethodOauth20 SourceAirtableUpdateAuthMethod = "oauth2.0"
)

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

type SourceAirtableUpdateOAuth20 struct {
	// Access Token for making authenticated requests.
	AccessToken *string                         `json:"access_token,omitempty"`
	authMethod  *SourceAirtableUpdateAuthMethod `const:"oauth2.0" json:"auth_method,omitempty"`
	// The client ID of the Airtable developer application.
	ClientID string `json:"client_id"`
	// The client secret of the Airtable developer application.
	ClientSecret string `json:"client_secret"`
	// The key to refresh the expired access token.
	RefreshToken string `json:"refresh_token"`
	// The date-time when the access token should be refreshed.
	TokenExpiryDate *time.Time `json:"token_expiry_date,omitempty"`
}

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

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

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

func (o *SourceAirtableUpdateOAuth20) GetAuthMethod() *SourceAirtableUpdateAuthMethod {
	return SourceAirtableUpdateAuthMethodOauth20.ToPointer()
}

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

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

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

func (o *SourceAirtableUpdateOAuth20) GetTokenExpiryDate() *time.Time {
	if o == nil {
		return nil
	}
	return o.TokenExpiryDate
}

type SourceAirtableUpdateAuthenticationType string

const (
	SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdateOAuth20             SourceAirtableUpdateAuthenticationType = "source-airtable-update_OAuth2.0"
	SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdatePersonalAccessToken SourceAirtableUpdateAuthenticationType = "source-airtable-update_Personal Access Token"
)

type SourceAirtableUpdateAuthentication struct {
	SourceAirtableUpdateOAuth20             *SourceAirtableUpdateOAuth20             `queryParam:"inline"`
	SourceAirtableUpdatePersonalAccessToken *SourceAirtableUpdatePersonalAccessToken `queryParam:"inline"`

	Type SourceAirtableUpdateAuthenticationType
}

func CreateSourceAirtableUpdateAuthenticationSourceAirtableUpdateOAuth20(sourceAirtableUpdateOAuth20 SourceAirtableUpdateOAuth20) SourceAirtableUpdateAuthentication {
	typ := SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdateOAuth20

	return SourceAirtableUpdateAuthentication{
		SourceAirtableUpdateOAuth20: &sourceAirtableUpdateOAuth20,
		Type:                        typ,
	}
}

func CreateSourceAirtableUpdateAuthenticationSourceAirtableUpdatePersonalAccessToken(sourceAirtableUpdatePersonalAccessToken SourceAirtableUpdatePersonalAccessToken) SourceAirtableUpdateAuthentication {
	typ := SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdatePersonalAccessToken

	return SourceAirtableUpdateAuthentication{
		SourceAirtableUpdatePersonalAccessToken: &sourceAirtableUpdatePersonalAccessToken,
		Type:                                    typ,
	}
}

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

	var sourceAirtableUpdatePersonalAccessToken SourceAirtableUpdatePersonalAccessToken = SourceAirtableUpdatePersonalAccessToken{}
	if err := utils.UnmarshalJSON(data, &sourceAirtableUpdatePersonalAccessToken, "", true, true); err == nil {
		u.SourceAirtableUpdatePersonalAccessToken = &sourceAirtableUpdatePersonalAccessToken
		u.Type = SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdatePersonalAccessToken
		return nil
	}

	var sourceAirtableUpdateOAuth20 SourceAirtableUpdateOAuth20 = SourceAirtableUpdateOAuth20{}
	if err := utils.UnmarshalJSON(data, &sourceAirtableUpdateOAuth20, "", true, true); err == nil {
		u.SourceAirtableUpdateOAuth20 = &sourceAirtableUpdateOAuth20
		u.Type = SourceAirtableUpdateAuthenticationTypeSourceAirtableUpdateOAuth20
		return nil
	}

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

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

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

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

type SourceAirtableUpdate struct {
	Credentials *SourceAirtableUpdateAuthentication `json:"credentials,omitempty"`
}

func (o *SourceAirtableUpdate) GetCredentials() *SourceAirtableUpdateAuthentication {
	if o == nil {
		return nil
	}
	return o.Credentials
}
