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

const (
	SourceDriftCredentialsAccessToken SourceDriftCredentials = "access_token"
)

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

type AccessToken struct {
	// Drift Access Token. See the <a href="https://docs.airbyte.com/integrations/sources/drift">docs</a> for more information on how to generate this key.
	AccessToken string                  `json:"access_token"`
	credentials *SourceDriftCredentials `const:"access_token" json:"credentials,omitempty"`
}

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

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

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

func (o *AccessToken) GetCredentials() *SourceDriftCredentials {
	return SourceDriftCredentialsAccessToken.ToPointer()
}

type SourceDriftSchemasCredentials string

const (
	SourceDriftSchemasCredentialsOauth20 SourceDriftSchemasCredentials = "oauth2.0"
)

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

type SourceDriftOAuth20 struct {
	// Access Token for making authenticated requests.
	AccessToken string `json:"access_token"`
	// The Client ID of your Drift developer application.
	ClientID string `json:"client_id"`
	// The Client Secret of your Drift developer application.
	ClientSecret string                         `json:"client_secret"`
	credentials  *SourceDriftSchemasCredentials `const:"oauth2.0" json:"credentials,omitempty"`
	// Refresh Token to renew the expired Access Token.
	RefreshToken string `json:"refresh_token"`
}

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

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

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

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

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

func (o *SourceDriftOAuth20) GetCredentials() *SourceDriftSchemasCredentials {
	return SourceDriftSchemasCredentialsOauth20.ToPointer()
}

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

type SourceDriftAuthorizationMethodType string

const (
	SourceDriftAuthorizationMethodTypeSourceDriftOAuth20 SourceDriftAuthorizationMethodType = "source-drift_OAuth2.0"
	SourceDriftAuthorizationMethodTypeAccessToken        SourceDriftAuthorizationMethodType = "Access Token"
)

type SourceDriftAuthorizationMethod struct {
	SourceDriftOAuth20 *SourceDriftOAuth20 `queryParam:"inline"`
	AccessToken        *AccessToken        `queryParam:"inline"`

	Type SourceDriftAuthorizationMethodType
}

func CreateSourceDriftAuthorizationMethodSourceDriftOAuth20(sourceDriftOAuth20 SourceDriftOAuth20) SourceDriftAuthorizationMethod {
	typ := SourceDriftAuthorizationMethodTypeSourceDriftOAuth20

	return SourceDriftAuthorizationMethod{
		SourceDriftOAuth20: &sourceDriftOAuth20,
		Type:               typ,
	}
}

func CreateSourceDriftAuthorizationMethodAccessToken(accessToken AccessToken) SourceDriftAuthorizationMethod {
	typ := SourceDriftAuthorizationMethodTypeAccessToken

	return SourceDriftAuthorizationMethod{
		AccessToken: &accessToken,
		Type:        typ,
	}
}

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

	var accessToken AccessToken = AccessToken{}
	if err := utils.UnmarshalJSON(data, &accessToken, "", true, true); err == nil {
		u.AccessToken = &accessToken
		u.Type = SourceDriftAuthorizationMethodTypeAccessToken
		return nil
	}

	var sourceDriftOAuth20 SourceDriftOAuth20 = SourceDriftOAuth20{}
	if err := utils.UnmarshalJSON(data, &sourceDriftOAuth20, "", true, true); err == nil {
		u.SourceDriftOAuth20 = &sourceDriftOAuth20
		u.Type = SourceDriftAuthorizationMethodTypeSourceDriftOAuth20
		return nil
	}

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

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

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

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

type Drift string

const (
	DriftDrift Drift = "drift"
)

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

type SourceDrift struct {
	Credentials *SourceDriftAuthorizationMethod `json:"credentials,omitempty"`
	// Email used as parameter for contacts stream
	Email      *string `default:"test@test.com" json:"email"`
	sourceType Drift   `const:"drift" json:"sourceType"`
}

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

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

func (o *SourceDrift) GetCredentials() *SourceDriftAuthorizationMethod {
	if o == nil {
		return nil
	}
	return o.Credentials
}

func (o *SourceDrift) GetEmail() *string {
	if o == nil {
		return nil
	}
	return o.Email
}

func (o *SourceDrift) GetSourceType() Drift {
	return DriftDrift
}
