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

const (
	RestRest Rest = "REST"
)

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

type RESTAPIStreams struct {
	// Follow the instructions in the "OAuth 2.0 in Postman - API Client for Integrations" article in the Workday community docs to obtain access token.
	AccessToken string `json:"access_token"`
	authType    Rest   `const:"REST" json:"auth_type"`
	// Rows after this date will be synced, default 2 years ago.
	StartDate *time.Time `json:"start_date,omitempty"`
}

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

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

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

func (o *RESTAPIStreams) GetAuthType() Rest {
	return RestRest
}

func (o *RESTAPIStreams) GetStartDate() *time.Time {
	if o == nil {
		return nil
	}
	return o.StartDate
}

type Raas string

const (
	RaasRaas Raas = "RAAS"
)

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

type ReportBasedStreams struct {
	authType Raas   `const:"RAAS" json:"auth_type"`
	Password string `json:"password"`
	// Report IDs can be found by clicking the three dots on the right side of the report > Web Service > View URLs > in JSON url copy everything between Workday tenant/ and ?format=json.
	ReportIds []any  `json:"report_ids"`
	Username  string `json:"username"`
}

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

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

func (o *ReportBasedStreams) GetAuthType() Raas {
	return RaasRaas
}

func (o *ReportBasedStreams) GetPassword() string {
	if o == nil {
		return ""
	}
	return o.Password
}

func (o *ReportBasedStreams) GetReportIds() []any {
	if o == nil {
		return []any{}
	}
	return o.ReportIds
}

func (o *ReportBasedStreams) GetUsername() string {
	if o == nil {
		return ""
	}
	return o.Username
}

type SourceWorkdayAuthenticationType string

const (
	SourceWorkdayAuthenticationTypeReportBasedStreams SourceWorkdayAuthenticationType = "Report Based Streams"
	SourceWorkdayAuthenticationTypeRESTAPIStreams     SourceWorkdayAuthenticationType = "REST API Streams"
)

// SourceWorkdayAuthentication - Report Based Streams and REST API Streams use different methods of Authentication. Choose streams type you want to sync and provide needed credentials for them.
type SourceWorkdayAuthentication struct {
	ReportBasedStreams *ReportBasedStreams `queryParam:"inline"`
	RESTAPIStreams     *RESTAPIStreams     `queryParam:"inline"`

	Type SourceWorkdayAuthenticationType
}

func CreateSourceWorkdayAuthenticationReportBasedStreams(reportBasedStreams ReportBasedStreams) SourceWorkdayAuthentication {
	typ := SourceWorkdayAuthenticationTypeReportBasedStreams

	return SourceWorkdayAuthentication{
		ReportBasedStreams: &reportBasedStreams,
		Type:               typ,
	}
}

func CreateSourceWorkdayAuthenticationRESTAPIStreams(restAPIStreams RESTAPIStreams) SourceWorkdayAuthentication {
	typ := SourceWorkdayAuthenticationTypeRESTAPIStreams

	return SourceWorkdayAuthentication{
		RESTAPIStreams: &restAPIStreams,
		Type:           typ,
	}
}

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

	var restAPIStreams RESTAPIStreams = RESTAPIStreams{}
	if err := utils.UnmarshalJSON(data, &restAPIStreams, "", true, true); err == nil {
		u.RESTAPIStreams = &restAPIStreams
		u.Type = SourceWorkdayAuthenticationTypeRESTAPIStreams
		return nil
	}

	var reportBasedStreams ReportBasedStreams = ReportBasedStreams{}
	if err := utils.UnmarshalJSON(data, &reportBasedStreams, "", true, true); err == nil {
		u.ReportBasedStreams = &reportBasedStreams
		u.Type = SourceWorkdayAuthenticationTypeReportBasedStreams
		return nil
	}

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

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

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

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

type Workday string

const (
	WorkdayWorkday Workday = "workday"
)

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

type SourceWorkday struct {
	// Report Based Streams and REST API Streams use different methods of Authentication. Choose streams type you want to sync and provide needed credentials for them.
	Credentials SourceWorkdayAuthentication `json:"credentials"`
	Host        string                      `json:"host"`
	TenantID    string                      `json:"tenant_id"`
	sourceType  Workday                     `const:"workday" json:"sourceType"`
}

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

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

func (o *SourceWorkday) GetCredentials() SourceWorkdayAuthentication {
	if o == nil {
		return SourceWorkdayAuthentication{}
	}
	return o.Credentials
}

func (o *SourceWorkday) GetHost() string {
	if o == nil {
		return ""
	}
	return o.Host
}

func (o *SourceWorkday) GetTenantID() string {
	if o == nil {
		return ""
	}
	return o.TenantID
}

func (o *SourceWorkday) GetSourceType() Workday {
	return WorkdayWorkday
}
