package automation_run

import (
	files_sdk "github.com/Files-com/files-sdk-go/v3"
	lib "github.com/Files-com/files-sdk-go/v3/lib"
	listquery "github.com/Files-com/files-sdk-go/v3/listquery"
)

type Client struct {
	files_sdk.Config
}

type Iter struct {
	*files_sdk.Iter
	*Client
}

func (i *Iter) Reload(opts ...files_sdk.RequestResponseOption) files_sdk.IterI {
	return &Iter{Iter: i.Iter.Reload(opts...).(*files_sdk.Iter), Client: i.Client}
}

func (i *Iter) AutomationRun() files_sdk.AutomationRun {
	return i.Current().(files_sdk.AutomationRun)
}

func (i *Iter) LoadResource(identifier interface{}, opts ...files_sdk.RequestResponseOption) (interface{}, error) {
	params := files_sdk.AutomationRunFindParams{}
	if id, ok := identifier.(int64); ok {
		params.Id = id
	}
	return i.Client.Find(params, opts...)
}

func (c *Client) List(params files_sdk.AutomationRunListParams, opts ...files_sdk.RequestResponseOption) (*Iter, error) {
	i := &Iter{Iter: &files_sdk.Iter{}, Client: c}
	path, err := lib.BuildPath("/automation_runs", params)
	if err != nil {
		return i, err
	}
	i.ListParams = &params
	list := files_sdk.AutomationRunCollection{}
	i.Query = listquery.Build(c.Config, path, &list, opts...)
	return i, nil
}

func List(params files_sdk.AutomationRunListParams, opts ...files_sdk.RequestResponseOption) (*Iter, error) {
	return (&Client{}).List(params, opts...)
}

func (c *Client) Find(params files_sdk.AutomationRunFindParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	err = files_sdk.Resource(c.Config, lib.Resource{Method: "GET", Path: "/automation_runs/{id}", Params: params, Entity: &automationRun}, opts...)
	return
}

func Find(params files_sdk.AutomationRunFindParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	return (&Client{}).Find(params, opts...)
}

func (c *Client) FindNode(params files_sdk.AutomationRunFindNodeParams, opts ...files_sdk.RequestResponseOption) (automationExecutionNode files_sdk.AutomationExecutionNode, err error) {
	err = files_sdk.Resource(c.Config, lib.Resource{Method: "GET", Path: "/automation_runs/{id}/node", Params: params, Entity: &automationExecutionNode}, opts...)
	return
}

func FindNode(params files_sdk.AutomationRunFindNodeParams, opts ...files_sdk.RequestResponseOption) (automationExecutionNode files_sdk.AutomationExecutionNode, err error) {
	return (&Client{}).FindNode(params, opts...)
}

func (c *Client) Cancel(params files_sdk.AutomationRunCancelParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	err = files_sdk.Resource(c.Config, lib.Resource{Method: "POST", Path: "/automation_runs/{id}/cancel", Params: params, Entity: &automationRun}, opts...)
	return
}

func Cancel(params files_sdk.AutomationRunCancelParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	return (&Client{}).Cancel(params, opts...)
}

func (c *Client) Rerun(params files_sdk.AutomationRunRerunParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	err = files_sdk.Resource(c.Config, lib.Resource{Method: "POST", Path: "/automation_runs/{id}/rerun", Params: params, Entity: &automationRun}, opts...)
	return
}

func Rerun(params files_sdk.AutomationRunRerunParams, opts ...files_sdk.RequestResponseOption) (automationRun files_sdk.AutomationRun, err error) {
	return (&Client{}).Rerun(params, opts...)
}
