//go:build !ignore_autogenerated
// +build !ignore_autogenerated

// Code generated by mga tool. DO NOT EDIT.

package clusterdriver

import (
	"context"
	"errors"
	"github.com/banzaicloud/pipeline/internal/cluster"
	"github.com/go-kit/kit/endpoint"
	kitxendpoint "github.com/sagikazarmark/kitx/endpoint"
)

// endpointError identifies an error that should be returned as an endpoint error.
type endpointError interface {
	EndpointError() bool
}

// serviceError identifies an error that should be returned as a service error.
type serviceError interface {
	ServiceError() bool
}

// Endpoints collects all of the endpoints that compose the underlying service. It's
// meant to be used as a helper struct, to collect all of the endpoints into a
// single parameter.
type Endpoints struct {
	CreateNodePools endpoint.Endpoint
	DeleteCluster   endpoint.Endpoint
	DeleteNodePool  endpoint.Endpoint
	ListNodePools   endpoint.Endpoint
	UpdateCluster   endpoint.Endpoint
	UpdateNodePool  endpoint.Endpoint
}

// MakeEndpoints returns a(n) Endpoints struct where each endpoint invokes
// the corresponding method on the provided service.
func MakeEndpoints(service cluster.Service, middleware ...endpoint.Middleware) Endpoints {
	mw := kitxendpoint.Combine(middleware...)

	return Endpoints{
		CreateNodePools: kitxendpoint.OperationNameMiddleware("cluster.CreateNodePools")(mw(MakeCreateNodePoolsEndpoint(service))),
		DeleteCluster:   kitxendpoint.OperationNameMiddleware("cluster.DeleteCluster")(mw(MakeDeleteClusterEndpoint(service))),
		DeleteNodePool:  kitxendpoint.OperationNameMiddleware("cluster.DeleteNodePool")(mw(MakeDeleteNodePoolEndpoint(service))),
		ListNodePools:   kitxendpoint.OperationNameMiddleware("cluster.ListNodePools")(mw(MakeListNodePoolsEndpoint(service))),
		UpdateCluster:   kitxendpoint.OperationNameMiddleware("cluster.UpdateCluster")(mw(MakeUpdateClusterEndpoint(service))),
		UpdateNodePool:  kitxendpoint.OperationNameMiddleware("cluster.UpdateNodePool")(mw(MakeUpdateNodePoolEndpoint(service))),
	}
}

// CreateNodePoolsRequest is a request struct for CreateNodePools endpoint.
type CreateNodePoolsRequest struct {
	ClusterID    uint
	RawNodePools map[string]cluster.NewRawNodePool
}

// CreateNodePoolsResponse is a response struct for CreateNodePools endpoint.
type CreateNodePoolsResponse struct {
	Err error
}

func (r CreateNodePoolsResponse) Failed() error {
	return r.Err
}

// MakeCreateNodePoolsEndpoint returns an endpoint for the matching method of the underlying service.
func MakeCreateNodePoolsEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(CreateNodePoolsRequest)

		err := service.CreateNodePools(ctx, req.ClusterID, req.RawNodePools)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return CreateNodePoolsResponse{Err: err}, nil
			}

			return CreateNodePoolsResponse{Err: err}, err
		}

		return CreateNodePoolsResponse{}, nil
	}
}

// DeleteClusterRequest is a request struct for DeleteCluster endpoint.
type DeleteClusterRequest struct {
	ClusterIdentifier cluster.Identifier
	Options           cluster.DeleteClusterOptions
}

// DeleteClusterResponse is a response struct for DeleteCluster endpoint.
type DeleteClusterResponse struct {
	Deleted bool
	Err     error
}

func (r DeleteClusterResponse) Failed() error {
	return r.Err
}

// MakeDeleteClusterEndpoint returns an endpoint for the matching method of the underlying service.
func MakeDeleteClusterEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(DeleteClusterRequest)

		deleted, err := service.DeleteCluster(ctx, req.ClusterIdentifier, req.Options)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return DeleteClusterResponse{
					Deleted: deleted,
					Err:     err,
				}, nil
			}

			return DeleteClusterResponse{
				Deleted: deleted,
				Err:     err,
			}, err
		}

		return DeleteClusterResponse{Deleted: deleted}, nil
	}
}

// DeleteNodePoolRequest is a request struct for DeleteNodePool endpoint.
type DeleteNodePoolRequest struct {
	ClusterID uint
	Name      string
}

// DeleteNodePoolResponse is a response struct for DeleteNodePool endpoint.
type DeleteNodePoolResponse struct {
	Deleted bool
	Err     error
}

func (r DeleteNodePoolResponse) Failed() error {
	return r.Err
}

// MakeDeleteNodePoolEndpoint returns an endpoint for the matching method of the underlying service.
func MakeDeleteNodePoolEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(DeleteNodePoolRequest)

		deleted, err := service.DeleteNodePool(ctx, req.ClusterID, req.Name)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return DeleteNodePoolResponse{
					Deleted: deleted,
					Err:     err,
				}, nil
			}

			return DeleteNodePoolResponse{
				Deleted: deleted,
				Err:     err,
			}, err
		}

		return DeleteNodePoolResponse{Deleted: deleted}, nil
	}
}

// ListNodePoolsRequest is a request struct for ListNodePools endpoint.
type ListNodePoolsRequest struct {
	ClusterID uint
}

// ListNodePoolsResponse is a response struct for ListNodePools endpoint.
type ListNodePoolsResponse struct {
	NodePoolList cluster.RawNodePoolList
	Err          error
}

func (r ListNodePoolsResponse) Failed() error {
	return r.Err
}

// MakeListNodePoolsEndpoint returns an endpoint for the matching method of the underlying service.
func MakeListNodePoolsEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(ListNodePoolsRequest)

		nodePoolList, err := service.ListNodePools(ctx, req.ClusterID)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return ListNodePoolsResponse{
					Err:          err,
					NodePoolList: nodePoolList,
				}, nil
			}

			return ListNodePoolsResponse{
				Err:          err,
				NodePoolList: nodePoolList,
			}, err
		}

		return ListNodePoolsResponse{NodePoolList: nodePoolList}, nil
	}
}

// UpdateClusterRequest is a request struct for UpdateCluster endpoint.
type UpdateClusterRequest struct {
	ClusterIdentifier cluster.Identifier
	ClusterUpdate     cluster.ClusterUpdate
}

// UpdateClusterResponse is a response struct for UpdateCluster endpoint.
type UpdateClusterResponse struct {
	Err error
}

func (r UpdateClusterResponse) Failed() error {
	return r.Err
}

// MakeUpdateClusterEndpoint returns an endpoint for the matching method of the underlying service.
func MakeUpdateClusterEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(UpdateClusterRequest)

		err := service.UpdateCluster(ctx, req.ClusterIdentifier, req.ClusterUpdate)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return UpdateClusterResponse{Err: err}, nil
			}

			return UpdateClusterResponse{Err: err}, err
		}

		return UpdateClusterResponse{}, nil
	}
}

// UpdateNodePoolRequest is a request struct for UpdateNodePool endpoint.
type UpdateNodePoolRequest struct {
	ClusterID         uint
	NodePoolName      string
	RawNodePoolUpdate cluster.RawNodePoolUpdate
}

// UpdateNodePoolResponse is a response struct for UpdateNodePool endpoint.
type UpdateNodePoolResponse struct {
	ProcessID string
	Err       error
}

func (r UpdateNodePoolResponse) Failed() error {
	return r.Err
}

// MakeUpdateNodePoolEndpoint returns an endpoint for the matching method of the underlying service.
func MakeUpdateNodePoolEndpoint(service cluster.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(UpdateNodePoolRequest)

		processID, err := service.UpdateNodePool(ctx, req.ClusterID, req.NodePoolName, req.RawNodePoolUpdate)

		if err != nil {
			if serviceErr := serviceError(nil); errors.As(err, &serviceErr) && serviceErr.ServiceError() {
				return UpdateNodePoolResponse{
					Err:       err,
					ProcessID: processID,
				}, nil
			}

			return UpdateNodePoolResponse{
				Err:       err,
				ProcessID: processID,
			}, err
		}

		return UpdateNodePoolResponse{ProcessID: processID}, nil
	}
}
