// 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"
)

type SourceRetentlySchemasAuthType string

const (
	SourceRetentlySchemasAuthTypeToken SourceRetentlySchemasAuthType = "Token"
)

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

type AuthenticateWithAPIToken struct {
	// Retently API Token. See the <a href="https://app.retently.com/settings/api/tokens">docs</a> for more information on how to obtain this key.
	APIKey               string                         `json:"api_key"`
	authType             *SourceRetentlySchemasAuthType `const:"Token" json:"auth_type,omitempty"`
	AdditionalProperties any                            `additionalProperties:"true" json:"-"`
}

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

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

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

func (o *AuthenticateWithAPIToken) GetAuthType() *SourceRetentlySchemasAuthType {
	return SourceRetentlySchemasAuthTypeToken.ToPointer()
}

func (o *AuthenticateWithAPIToken) GetAdditionalProperties() any {
	if o == nil {
		return nil
	}
	return o.AdditionalProperties
}

type SourceRetentlyAuthType string

const (
	SourceRetentlyAuthTypeClient SourceRetentlyAuthType = "Client"
)

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

type AuthenticateViaRetentlyOAuth struct {
	authType *SourceRetentlyAuthType `const:"Client" json:"auth_type,omitempty"`
	// The Client ID of your Retently developer application.
	ClientID string `json:"client_id"`
	// The Client Secret of your Retently developer application.
	ClientSecret string `json:"client_secret"`
	// Retently Refresh Token which can be used to fetch new Bearer Tokens when the current one expires.
	RefreshToken         string `json:"refresh_token"`
	AdditionalProperties any    `additionalProperties:"true" json:"-"`
}

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

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

func (o *AuthenticateViaRetentlyOAuth) GetAuthType() *SourceRetentlyAuthType {
	return SourceRetentlyAuthTypeClient.ToPointer()
}

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

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

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

func (o *AuthenticateViaRetentlyOAuth) GetAdditionalProperties() any {
	if o == nil {
		return nil
	}
	return o.AdditionalProperties
}

type SourceRetentlyAuthenticationMechanismType string

const (
	SourceRetentlyAuthenticationMechanismTypeAuthenticateViaRetentlyOAuth SourceRetentlyAuthenticationMechanismType = "Authenticate via Retently (OAuth)"
	SourceRetentlyAuthenticationMechanismTypeAuthenticateWithAPIToken     SourceRetentlyAuthenticationMechanismType = "Authenticate with API Token"
)

// SourceRetentlyAuthenticationMechanism - Choose how to authenticate to Retently
type SourceRetentlyAuthenticationMechanism struct {
	AuthenticateViaRetentlyOAuth *AuthenticateViaRetentlyOAuth `queryParam:"inline"`
	AuthenticateWithAPIToken     *AuthenticateWithAPIToken     `queryParam:"inline"`

	Type SourceRetentlyAuthenticationMechanismType
}

func CreateSourceRetentlyAuthenticationMechanismAuthenticateViaRetentlyOAuth(authenticateViaRetentlyOAuth AuthenticateViaRetentlyOAuth) SourceRetentlyAuthenticationMechanism {
	typ := SourceRetentlyAuthenticationMechanismTypeAuthenticateViaRetentlyOAuth

	return SourceRetentlyAuthenticationMechanism{
		AuthenticateViaRetentlyOAuth: &authenticateViaRetentlyOAuth,
		Type:                         typ,
	}
}

func CreateSourceRetentlyAuthenticationMechanismAuthenticateWithAPIToken(authenticateWithAPIToken AuthenticateWithAPIToken) SourceRetentlyAuthenticationMechanism {
	typ := SourceRetentlyAuthenticationMechanismTypeAuthenticateWithAPIToken

	return SourceRetentlyAuthenticationMechanism{
		AuthenticateWithAPIToken: &authenticateWithAPIToken,
		Type:                     typ,
	}
}

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

	var authenticateWithAPIToken AuthenticateWithAPIToken = AuthenticateWithAPIToken{}
	if err := utils.UnmarshalJSON(data, &authenticateWithAPIToken, "", true, true); err == nil {
		u.AuthenticateWithAPIToken = &authenticateWithAPIToken
		u.Type = SourceRetentlyAuthenticationMechanismTypeAuthenticateWithAPIToken
		return nil
	}

	var authenticateViaRetentlyOAuth AuthenticateViaRetentlyOAuth = AuthenticateViaRetentlyOAuth{}
	if err := utils.UnmarshalJSON(data, &authenticateViaRetentlyOAuth, "", true, true); err == nil {
		u.AuthenticateViaRetentlyOAuth = &authenticateViaRetentlyOAuth
		u.Type = SourceRetentlyAuthenticationMechanismTypeAuthenticateViaRetentlyOAuth
		return nil
	}

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

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

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

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

type Retently string

const (
	RetentlyRetently Retently = "retently"
)

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

type SourceRetently struct {
	// Choose how to authenticate to Retently
	Credentials *SourceRetentlyAuthenticationMechanism `json:"credentials,omitempty"`
	sourceType  Retently                               `const:"retently" json:"sourceType"`
}

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

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

func (o *SourceRetently) GetCredentials() *SourceRetentlyAuthenticationMechanism {
	if o == nil {
		return nil
	}
	return o.Credentials
}

func (o *SourceRetently) GetSourceType() Retently {
	return RetentlyRetently
}
