/*
Copyright (C) 2025 filifa

This file is part of mlbstats.

mlbstats is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

mlbstats is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
mlbstats. If not, see <https://www.gnu.org/licenses/>.
*/

/*
 * Stats API Documentation
 *
 * Official API for Major League Baseball.
 *
 * API version: 2.0.0
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
package api

import (
	"context"
	"github.com/antihax/optional"
	"io/ioutil"
	"net/http"
	"net/url"
	"scm.dairydemon.net/filifa/mlbstats/api/models"
	"strings"
)

// Linger please
var (
	_ context.Context
)

type TransactionsApiService service

/*
TransactionsApiService View transaction info
This endpoint allows you to pull transactions.
 * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 * @param optional nil or *TransactionsApiTransactionsOpts - Optional Parameters:
     * @param "LeagueId" (optional.Interface of []int32) -  Comma delimited list of Unique league identifiers
     * @param "SportId" (optional.Interface of []int32) -  Comma delimited list of top level organizations of a sport
     * @param "TeamId" (optional.Interface of []int32) -  Comma delimited list of Unique Team identifiers
     * @param "PlayerId" (optional.Interface of []int32) -  A unique identifier for a player
     * @param "Date" (optional.String) -  Date of Game. Format: YYYY-MM-DD
     * @param "StartDate" (optional.String) -  Start date for range of data (must be used with end date). Format: MM/DD/YYYY
     * @param "EndDate" (optional.String) -  End date for range of data (must be used with start date). Format: MM/DD/YYYY
     * @param "TransactionIds" (optional.Interface of []int32) -
     * @param "TransactionTypes" (optional.Interface of []string) -
     * @param "DivisionIds" (optional.Interface of []int32) -
     * @param "Order" (optional.Interface of SortOrderEnum) -
     * @param "Limit" (optional.Int32) -
     * @param "Fields" (optional.Interface of []string) -  Comma delimited list of specific fields to be returned. Format: topLevelNode, childNode, attribute
@return models.TransactionsRestObject
*/

type TransactionsApiTransactionsOpts struct {
	LeagueId         optional.Interface
	SportId          optional.Interface
	TeamId           optional.Interface
	PlayerId         optional.Interface
	Date             optional.String
	StartDate        optional.String
	EndDate          optional.String
	TransactionIds   optional.Interface
	TransactionTypes optional.Interface
	DivisionIds      optional.Interface
	Order            optional.Interface
	Limit            optional.Int32
	Fields           optional.Interface
}

func (a *TransactionsApiService) Transactions(ctx context.Context, localVarOptionals *TransactionsApiTransactionsOpts) (models.TransactionsRestObject, *http.Response, error) {
	var (
		localVarHttpMethod  = strings.ToUpper("Get")
		localVarPostBody    interface{}
		localVarFileName    string
		localVarFileBytes   []byte
		localVarReturnValue models.TransactionsRestObject
	)

	// create path and map variables
	localVarPath := a.client.cfg.BasePath + "/api/v1/transactions"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	if localVarOptionals != nil && localVarOptionals.LeagueId.IsSet() {
		localVarQueryParams.Add("leagueId", parameterToString(localVarOptionals.LeagueId.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.SportId.IsSet() {
		localVarQueryParams.Add("sportId", parameterToString(localVarOptionals.SportId.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.TeamId.IsSet() {
		localVarQueryParams.Add("teamId", parameterToString(localVarOptionals.TeamId.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.PlayerId.IsSet() {
		localVarQueryParams.Add("playerId", parameterToString(localVarOptionals.PlayerId.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.Date.IsSet() {
		localVarQueryParams.Add("date", parameterToString(localVarOptionals.Date.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.StartDate.IsSet() {
		localVarQueryParams.Add("startDate", parameterToString(localVarOptionals.StartDate.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.EndDate.IsSet() {
		localVarQueryParams.Add("endDate", parameterToString(localVarOptionals.EndDate.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.TransactionIds.IsSet() {
		localVarQueryParams.Add("transactionIds", parameterToString(localVarOptionals.TransactionIds.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.TransactionTypes.IsSet() {
		localVarQueryParams.Add("transactionTypes", parameterToString(localVarOptionals.TransactionTypes.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.DivisionIds.IsSet() {
		localVarQueryParams.Add("divisionIds", parameterToString(localVarOptionals.DivisionIds.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.Order.IsSet() {
		localVarQueryParams.Add("order", parameterToString(localVarOptionals.Order.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.Limit.IsSet() {
		localVarQueryParams.Add("limit", parameterToString(localVarOptionals.Limit.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.Fields.IsSet() {
		localVarQueryParams.Add("fields", parameterToString(localVarOptionals.Fields.Value(), "multi"))
	}
	// to determine the Content-Type header
	localVarHttpContentTypes := []string{}

	// set Content-Type header
	localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
	if localVarHttpContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHttpContentType
	}

	// to determine the Accept header
	localVarHttpHeaderAccepts := []string{"*/*"}

	// set Accept header
	localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
	if localVarHttpHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
	}
	r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHttpResponse, err := a.client.callAPI(r)
	if err != nil || localVarHttpResponse == nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
	localVarHttpResponse.Body.Close()
	if err != nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	if localVarHttpResponse.StatusCode < 300 {
		// If we succeed, return the data, otherwise pass on to decode error.
		err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
		if err == nil {
			return localVarReturnValue, localVarHttpResponse, err
		}
	}

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body:  localVarBody,
			error: localVarHttpResponse.Status,
		}
		if localVarHttpResponse.StatusCode == 200 {
			var v models.TransactionsRestObject
			err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHttpResponse, newErr
			}
			newErr.model = v
			return localVarReturnValue, localVarHttpResponse, newErr
		}
		return localVarReturnValue, localVarHttpResponse, newErr
	}

	return localVarReturnValue, localVarHttpResponse, nil
}
