// 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"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/types"
)

type SourceSquareSchemasAuthType string

const (
	SourceSquareSchemasAuthTypeAPIKey SourceSquareSchemasAuthType = "API Key"
)

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

type SourceSquareAPIKey struct {
	// The API key for a Square application
	APIKey   string                      `json:"api_key"`
	authType SourceSquareSchemasAuthType `const:"API Key" json:"auth_type"`
}

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

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

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

func (o *SourceSquareAPIKey) GetAuthType() SourceSquareSchemasAuthType {
	return SourceSquareSchemasAuthTypeAPIKey
}

type SourceSquareAuthType string

const (
	SourceSquareAuthTypeOAuth SourceSquareAuthType = "OAuth"
)

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

type OauthAuthentication struct {
	authType SourceSquareAuthType `const:"OAuth" json:"auth_type"`
	// The Square-issued ID of your application
	ClientID string `json:"client_id"`
	// The Square-issued application secret for your application
	ClientSecret string `json:"client_secret"`
	// A refresh token generated using the above client ID and secret
	RefreshToken string `json:"refresh_token"`
}

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

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

func (o *OauthAuthentication) GetAuthType() SourceSquareAuthType {
	return SourceSquareAuthTypeOAuth
}

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

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

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

type SourceSquareAuthenticationType string

const (
	SourceSquareAuthenticationTypeOauthAuthentication SourceSquareAuthenticationType = "Oauth authentication"
	SourceSquareAuthenticationTypeSourceSquareAPIKey  SourceSquareAuthenticationType = "source-square_API key"
)

// SourceSquareAuthentication - Choose how to authenticate to Square.
type SourceSquareAuthentication struct {
	OauthAuthentication *OauthAuthentication `queryParam:"inline"`
	SourceSquareAPIKey  *SourceSquareAPIKey  `queryParam:"inline"`

	Type SourceSquareAuthenticationType
}

func CreateSourceSquareAuthenticationOauthAuthentication(oauthAuthentication OauthAuthentication) SourceSquareAuthentication {
	typ := SourceSquareAuthenticationTypeOauthAuthentication

	return SourceSquareAuthentication{
		OauthAuthentication: &oauthAuthentication,
		Type:                typ,
	}
}

func CreateSourceSquareAuthenticationSourceSquareAPIKey(sourceSquareAPIKey SourceSquareAPIKey) SourceSquareAuthentication {
	typ := SourceSquareAuthenticationTypeSourceSquareAPIKey

	return SourceSquareAuthentication{
		SourceSquareAPIKey: &sourceSquareAPIKey,
		Type:               typ,
	}
}

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

	var sourceSquareAPIKey SourceSquareAPIKey = SourceSquareAPIKey{}
	if err := utils.UnmarshalJSON(data, &sourceSquareAPIKey, "", true, true); err == nil {
		u.SourceSquareAPIKey = &sourceSquareAPIKey
		u.Type = SourceSquareAuthenticationTypeSourceSquareAPIKey
		return nil
	}

	var oauthAuthentication OauthAuthentication = OauthAuthentication{}
	if err := utils.UnmarshalJSON(data, &oauthAuthentication, "", true, true); err == nil {
		u.OauthAuthentication = &oauthAuthentication
		u.Type = SourceSquareAuthenticationTypeOauthAuthentication
		return nil
	}

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

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

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

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

type Square string

const (
	SquareSquare Square = "square"
)

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

type SourceSquare struct {
	// Choose how to authenticate to Square.
	Credentials *SourceSquareAuthentication `json:"credentials,omitempty"`
	// In some streams there is an option to include deleted objects (Items, Categories, Discounts, Taxes)
	IncludeDeletedObjects *bool `default:"false" json:"include_deleted_objects"`
	// Determines whether to use the sandbox or production environment.
	IsSandbox *bool `default:"false" json:"is_sandbox"`
	// UTC date in the format YYYY-MM-DD. Any data before this date will not be replicated. If not set, all data will be replicated.
	StartDate  *types.Date `default:"2021-01-01" json:"start_date"`
	sourceType Square      `const:"square" json:"sourceType"`
}

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

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

func (o *SourceSquare) GetCredentials() *SourceSquareAuthentication {
	if o == nil {
		return nil
	}
	return o.Credentials
}

func (o *SourceSquare) GetIncludeDeletedObjects() *bool {
	if o == nil {
		return nil
	}
	return o.IncludeDeletedObjects
}

func (o *SourceSquare) GetIsSandbox() *bool {
	if o == nil {
		return nil
	}
	return o.IsSandbox
}

func (o *SourceSquare) GetStartDate() *types.Date {
	if o == nil {
		return nil
	}
	return o.StartDate
}

func (o *SourceSquare) GetSourceType() Square {
	return SquareSquare
}
