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

const (
	SourceCartSchemasAuthTypeSingleStoreAccessToken SourceCartSchemasAuthType = "SINGLE_STORE_ACCESS_TOKEN"
)

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

type SingleStoreAccessToken struct {
	// Access Token for making authenticated requests.
	AccessToken string                    `json:"access_token"`
	authType    SourceCartSchemasAuthType `const:"SINGLE_STORE_ACCESS_TOKEN" json:"auth_type"`
	// The name of Cart.com Online Store. All API URLs start with https://[mystorename.com]/api/v1/, where [mystorename.com] is the domain name of your store.
	StoreName string `json:"store_name"`
}

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

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

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

func (o *SingleStoreAccessToken) GetAuthType() SourceCartSchemasAuthType {
	return SourceCartSchemasAuthTypeSingleStoreAccessToken
}

func (o *SingleStoreAccessToken) GetStoreName() string {
	if o == nil {
		return ""
	}
	return o.StoreName
}

type SourceCartAuthType string

const (
	SourceCartAuthTypeCentralAPIRouter SourceCartAuthType = "CENTRAL_API_ROUTER"
)

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

type CentralAPIRouter struct {
	authType SourceCartAuthType `const:"CENTRAL_API_ROUTER" json:"auth_type"`
	// You can determine a site provisioning site Id by hitting https://site.com/store/sitemonitor.aspx and reading the response param PSID
	SiteID string `json:"site_id"`
	// Enter your application's User Name
	UserName string `json:"user_name"`
	// Enter your application's User Secret
	UserSecret string `json:"user_secret"`
}

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

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

func (o *CentralAPIRouter) GetAuthType() SourceCartAuthType {
	return SourceCartAuthTypeCentralAPIRouter
}

func (o *CentralAPIRouter) GetSiteID() string {
	if o == nil {
		return ""
	}
	return o.SiteID
}

func (o *CentralAPIRouter) GetUserName() string {
	if o == nil {
		return ""
	}
	return o.UserName
}

func (o *CentralAPIRouter) GetUserSecret() string {
	if o == nil {
		return ""
	}
	return o.UserSecret
}

type AuthorizationMethodType string

const (
	AuthorizationMethodTypeCentralAPIRouter       AuthorizationMethodType = "Central API Router"
	AuthorizationMethodTypeSingleStoreAccessToken AuthorizationMethodType = "Single Store Access Token"
)

type AuthorizationMethod struct {
	CentralAPIRouter       *CentralAPIRouter       `queryParam:"inline"`
	SingleStoreAccessToken *SingleStoreAccessToken `queryParam:"inline"`

	Type AuthorizationMethodType
}

func CreateAuthorizationMethodCentralAPIRouter(centralAPIRouter CentralAPIRouter) AuthorizationMethod {
	typ := AuthorizationMethodTypeCentralAPIRouter

	return AuthorizationMethod{
		CentralAPIRouter: &centralAPIRouter,
		Type:             typ,
	}
}

func CreateAuthorizationMethodSingleStoreAccessToken(singleStoreAccessToken SingleStoreAccessToken) AuthorizationMethod {
	typ := AuthorizationMethodTypeSingleStoreAccessToken

	return AuthorizationMethod{
		SingleStoreAccessToken: &singleStoreAccessToken,
		Type:                   typ,
	}
}

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

	var singleStoreAccessToken SingleStoreAccessToken = SingleStoreAccessToken{}
	if err := utils.UnmarshalJSON(data, &singleStoreAccessToken, "", true, true); err == nil {
		u.SingleStoreAccessToken = &singleStoreAccessToken
		u.Type = AuthorizationMethodTypeSingleStoreAccessToken
		return nil
	}

	var centralAPIRouter CentralAPIRouter = CentralAPIRouter{}
	if err := utils.UnmarshalJSON(data, &centralAPIRouter, "", true, true); err == nil {
		u.CentralAPIRouter = &centralAPIRouter
		u.Type = AuthorizationMethodTypeCentralAPIRouter
		return nil
	}

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

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

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

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

type Cart string

const (
	CartCart Cart = "cart"
)

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

type SourceCart struct {
	Credentials *AuthorizationMethod `json:"credentials,omitempty"`
	// The date from which you'd like to replicate the data
	StartDate  string `json:"start_date"`
	sourceType Cart   `const:"cart" json:"sourceType"`
}

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

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

func (o *SourceCart) GetCredentials() *AuthorizationMethod {
	if o == nil {
		return nil
	}
	return o.Credentials
}

func (o *SourceCart) GetStartDate() string {
	if o == nil {
		return ""
	}
	return o.StartDate
}

func (o *SourceCart) GetSourceType() Cart {
	return CartCart
}
