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

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

package integratedservicesdriver

import (
	"context"
	"errors"
	"github.com/banzaicloud/pipeline/internal/integratedservices"
	"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 {
	Activate   endpoint.Endpoint
	Deactivate endpoint.Endpoint
	Details    endpoint.Endpoint
	List       endpoint.Endpoint
	Update     endpoint.Endpoint
}

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

	return Endpoints{
		Activate:   kitxendpoint.OperationNameMiddleware("integratedservices.Activate")(mw(MakeActivateEndpoint(service))),
		Deactivate: kitxendpoint.OperationNameMiddleware("integratedservices.Deactivate")(mw(MakeDeactivateEndpoint(service))),
		Details:    kitxendpoint.OperationNameMiddleware("integratedservices.Details")(mw(MakeDetailsEndpoint(service))),
		List:       kitxendpoint.OperationNameMiddleware("integratedservices.List")(mw(MakeListEndpoint(service))),
		Update:     kitxendpoint.OperationNameMiddleware("integratedservices.Update")(mw(MakeUpdateEndpoint(service))),
	}
}

// ActivateRequest is a request struct for Activate endpoint.
type ActivateRequest struct {
	ClusterID   uint
	ServiceName string
	Spec        map[string]interface{}
}

// ActivateResponse is a response struct for Activate endpoint.
type ActivateResponse struct {
	Err error
}

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

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

		err := service.Activate(ctx, req.ClusterID, req.ServiceName, req.Spec)

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

			return ActivateResponse{Err: err}, err
		}

		return ActivateResponse{}, nil
	}
}

// DeactivateRequest is a request struct for Deactivate endpoint.
type DeactivateRequest struct {
	ClusterID   uint
	ServiceName string
}

// DeactivateResponse is a response struct for Deactivate endpoint.
type DeactivateResponse struct {
	Err error
}

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

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

		err := service.Deactivate(ctx, req.ClusterID, req.ServiceName)

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

			return DeactivateResponse{Err: err}, err
		}

		return DeactivateResponse{}, nil
	}
}

// DetailsRequest is a request struct for Details endpoint.
type DetailsRequest struct {
	ClusterID   uint
	ServiceName string
}

// DetailsResponse is a response struct for Details endpoint.
type DetailsResponse struct {
	Service integratedservices.IntegratedService
	Err     error
}

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

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

		service, err := service.Details(ctx, req.ClusterID, req.ServiceName)

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

			return DetailsResponse{
				Err:     err,
				Service: service,
			}, err
		}

		return DetailsResponse{Service: service}, nil
	}
}

// ListRequest is a request struct for List endpoint.
type ListRequest struct {
	ClusterID uint
}

// ListResponse is a response struct for List endpoint.
type ListResponse struct {
	Services []integratedservices.IntegratedService
	Err      error
}

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

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

		services, err := service.List(ctx, req.ClusterID)

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

			return ListResponse{
				Err:      err,
				Services: services,
			}, err
		}

		return ListResponse{Services: services}, nil
	}
}

// UpdateRequest is a request struct for Update endpoint.
type UpdateRequest struct {
	ClusterID   uint
	ServiceName string
	Spec        map[string]interface{}
}

// UpdateResponse is a response struct for Update endpoint.
type UpdateResponse struct {
	Err error
}

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

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

		err := service.Update(ctx, req.ClusterID, req.ServiceName, req.Spec)

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

			return UpdateResponse{Err: err}, err
		}

		return UpdateResponse{}, nil
	}
}
