// This file was auto-generated by Fern from our API Definition.

package runners

import (
	context "context"
	fmt "fmt"
	http "net/http"
	url "net/url"
	sdk "sdk"
	core "sdk/core"
)

type Client struct {
	baseURL string
	caller  *core.Caller
	header  http.Header
}

func NewClient(opts ...core.ClientOption) *Client {
	options := core.NewClientOptions()
	for _, opt := range opts {
		opt(options)
	}
	return &Client{
		baseURL: options.BaseURL,
		caller:  core.NewCaller(options.HTTPClient),
		header:  options.ToHeader(),
	}
}

func (c *Client) List(ctx context.Context, request *sdk.RunnersListRequest) (*sdk.RunnersListResponse, error) {
	baseURL := ""
	if c.baseURL != "" {
		baseURL = c.baseURL
	}
	endpointURL := baseURL + "/" + "runners"

	queryParams := make(url.Values)
	queryParams.Add("namespace", fmt.Sprintf("%v", request.Namespace))
	if request.Name != nil {
		queryParams.Add("name", fmt.Sprintf("%v", *request.Name))
	}
	if request.RunnerIds != nil {
		queryParams.Add("runner_ids", fmt.Sprintf("%v", *request.RunnerIds))
	}
	for _, value := range request.RunnerId {
		queryParams.Add("runner_id", fmt.Sprintf("%v", *value))
	}
	if request.IncludeStopped != nil {
		queryParams.Add("include_stopped", fmt.Sprintf("%v", *request.IncludeStopped))
	}
	if request.Limit != nil {
		queryParams.Add("limit", fmt.Sprintf("%v", *request.Limit))
	}
	if request.Cursor != nil {
		queryParams.Add("cursor", fmt.Sprintf("%v", *request.Cursor))
	}
	if len(queryParams) > 0 {
		endpointURL += "?" + queryParams.Encode()
	}

	var response *sdk.RunnersListResponse
	if err := c.caller.Call(
		ctx,
		&core.CallParams{
			URL:      endpointURL,
			Method:   http.MethodGet,
			Headers:  c.header,
			Response: &response,
		},
	); err != nil {
		return nil, err
	}
	return response, nil
}

// 2 round trips:
//
// - GET /runners/names (fanout)
// - [api-peer] namespace::ops::resolve_for_name_global
func (c *Client) ListNames(ctx context.Context, request *sdk.RunnersListNamesRequest) (*sdk.RunnersListNamesResponse, error) {
	baseURL := ""
	if c.baseURL != "" {
		baseURL = c.baseURL
	}
	endpointURL := baseURL + "/" + "runners/names"

	queryParams := make(url.Values)
	queryParams.Add("namespace", fmt.Sprintf("%v", request.Namespace))
	if request.Limit != nil {
		queryParams.Add("limit", fmt.Sprintf("%v", *request.Limit))
	}
	if request.Cursor != nil {
		queryParams.Add("cursor", fmt.Sprintf("%v", *request.Cursor))
	}
	if len(queryParams) > 0 {
		endpointURL += "?" + queryParams.Encode()
	}

	var response *sdk.RunnersListNamesResponse
	if err := c.caller.Call(
		ctx,
		&core.CallParams{
			URL:      endpointURL,
			Method:   http.MethodGet,
			Headers:  c.header,
			Response: &response,
		},
	); err != nil {
		return nil, err
	}
	return response, nil
}
