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

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

package processdriver

import (
	"context"
	"errors"
	pipeline "github.com/banzaicloud/pipeline/.gen/pipeline/pipeline"
	"github.com/banzaicloud/pipeline/internal/app/pipeline/process"
	"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
}

// WorkflowEndpoints 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 WorkflowEndpoints struct {
	CancelProcess   endpoint.Endpoint
	GetProcess      endpoint.Endpoint
	ListProcesses   endpoint.Endpoint
	LogProcess      endpoint.Endpoint
	LogProcessEvent endpoint.Endpoint
	SignalProcess   endpoint.Endpoint
}

// MakeWorkflowEndpoints returns a(n) WorkflowEndpoints struct where each endpoint invokes
// the corresponding method on the provided service.
func MakeWorkflowEndpoints(service process.WorkflowService, middleware ...endpoint.Middleware) WorkflowEndpoints {
	mw := kitxendpoint.Combine(middleware...)

	return WorkflowEndpoints{
		CancelProcess:   kitxendpoint.OperationNameMiddleware("process.Workflow.CancelProcess")(mw(MakeCancelProcessWorkflowEndpoint(service))),
		GetProcess:      kitxendpoint.OperationNameMiddleware("process.Workflow.GetProcess")(mw(MakeGetProcessWorkflowEndpoint(service))),
		ListProcesses:   kitxendpoint.OperationNameMiddleware("process.Workflow.ListProcesses")(mw(MakeListProcessesWorkflowEndpoint(service))),
		LogProcess:      kitxendpoint.OperationNameMiddleware("process.Workflow.LogProcess")(mw(MakeLogProcessWorkflowEndpoint(service))),
		LogProcessEvent: kitxendpoint.OperationNameMiddleware("process.Workflow.LogProcessEvent")(mw(MakeLogProcessEventWorkflowEndpoint(service))),
		SignalProcess:   kitxendpoint.OperationNameMiddleware("process.Workflow.SignalProcess")(mw(MakeSignalProcessWorkflowEndpoint(service))),
	}
}

// CancelProcessWorkflowRequest is a request struct for CancelProcess endpoint.
type CancelProcessWorkflowRequest struct {
	Id string
}

// CancelProcessWorkflowResponse is a response struct for CancelProcess endpoint.
type CancelProcessWorkflowResponse struct {
	Err error
}

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

// MakeCancelProcessWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeCancelProcessWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(CancelProcessWorkflowRequest)

		err := service.CancelProcess(ctx, req.Id)

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

			return CancelProcessWorkflowResponse{Err: err}, err
		}

		return CancelProcessWorkflowResponse{}, nil
	}
}

// GetProcessWorkflowRequest is a request struct for GetProcess endpoint.
type GetProcessWorkflowRequest struct {
	Id string
}

// GetProcessWorkflowResponse is a response struct for GetProcess endpoint.
type GetProcessWorkflowResponse struct {
	Process pipeline.Process
	Err     error
}

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

// MakeGetProcessWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeGetProcessWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(GetProcessWorkflowRequest)

		process, err := service.GetProcess(ctx, req.Id)

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

			return GetProcessWorkflowResponse{
				Err:     err,
				Process: process,
			}, err
		}

		return GetProcessWorkflowResponse{Process: process}, nil
	}
}

// ListProcessesWorkflowRequest is a request struct for ListProcesses endpoint.
type ListProcessesWorkflowRequest struct {
	Query pipeline.Process
}

// ListProcessesWorkflowResponse is a response struct for ListProcesses endpoint.
type ListProcessesWorkflowResponse struct {
	Processes []pipeline.Process
	Err       error
}

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

// MakeListProcessesWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeListProcessesWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(ListProcessesWorkflowRequest)

		processes, err := service.ListProcesses(ctx, req.Query)

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

			return ListProcessesWorkflowResponse{
				Err:       err,
				Processes: processes,
			}, err
		}

		return ListProcessesWorkflowResponse{Processes: processes}, nil
	}
}

// LogProcessWorkflowRequest is a request struct for LogProcess endpoint.
type LogProcessWorkflowRequest struct {
	Proc pipeline.Process
}

// LogProcessWorkflowResponse is a response struct for LogProcess endpoint.
type LogProcessWorkflowResponse struct {
	Process pipeline.Process
	Err     error
}

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

// MakeLogProcessWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeLogProcessWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(LogProcessWorkflowRequest)

		process, err := service.LogProcess(ctx, req.Proc)

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

			return LogProcessWorkflowResponse{
				Err:     err,
				Process: process,
			}, err
		}

		return LogProcessWorkflowResponse{Process: process}, nil
	}
}

// LogProcessEventWorkflowRequest is a request struct for LogProcessEvent endpoint.
type LogProcessEventWorkflowRequest struct {
	Proc pipeline.ProcessEvent
}

// LogProcessEventWorkflowResponse is a response struct for LogProcessEvent endpoint.
type LogProcessEventWorkflowResponse struct {
	ProcessEvent pipeline.ProcessEvent
	Err          error
}

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

// MakeLogProcessEventWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeLogProcessEventWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(LogProcessEventWorkflowRequest)

		processEvent, err := service.LogProcessEvent(ctx, req.Proc)

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

			return LogProcessEventWorkflowResponse{
				Err:          err,
				ProcessEvent: processEvent,
			}, err
		}

		return LogProcessEventWorkflowResponse{ProcessEvent: processEvent}, nil
	}
}

// SignalProcessWorkflowRequest is a request struct for SignalProcess endpoint.
type SignalProcessWorkflowRequest struct {
	Id     string
	Signal string
	Value  interface{}
}

// SignalProcessWorkflowResponse is a response struct for SignalProcess endpoint.
type SignalProcessWorkflowResponse struct {
	Err error
}

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

// MakeSignalProcessWorkflowEndpoint returns an endpoint for the matching method of the underlying service.
func MakeSignalProcessWorkflowEndpoint(service process.WorkflowService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(SignalProcessWorkflowRequest)

		err := service.SignalProcess(ctx, req.Id, req.Signal, req.Value)

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

			return SignalProcessWorkflowResponse{Err: err}, err
		}

		return SignalProcessWorkflowResponse{}, nil
	}
}
