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

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

package tokendriver

import (
	"context"
	"errors"
	"github.com/banzaicloud/pipeline/internal/app/pipeline/auth/token"
	"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 {
	CreateToken endpoint.Endpoint
	DeleteToken endpoint.Endpoint
	GetToken    endpoint.Endpoint
	ListTokens  endpoint.Endpoint
}

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

	return Endpoints{
		CreateToken: kitxendpoint.OperationNameMiddleware("token.CreateToken")(mw(MakeCreateTokenEndpoint(service))),
		DeleteToken: kitxendpoint.OperationNameMiddleware("token.DeleteToken")(mw(MakeDeleteTokenEndpoint(service))),
		GetToken:    kitxendpoint.OperationNameMiddleware("token.GetToken")(mw(MakeGetTokenEndpoint(service))),
		ListTokens:  kitxendpoint.OperationNameMiddleware("token.ListTokens")(mw(MakeListTokensEndpoint(service))),
	}
}

// CreateTokenRequest is a request struct for CreateToken endpoint.
type CreateTokenRequest struct {
	TokenRequest token.NewTokenRequest
}

// CreateTokenResponse is a response struct for CreateToken endpoint.
type CreateTokenResponse struct {
	NewToken token.NewToken
	Err      error
}

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

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

		newToken, err := service.CreateToken(ctx, req.TokenRequest)

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

			return CreateTokenResponse{
				Err:      err,
				NewToken: newToken,
			}, err
		}

		return CreateTokenResponse{NewToken: newToken}, nil
	}
}

// DeleteTokenRequest is a request struct for DeleteToken endpoint.
type DeleteTokenRequest struct {
	Id string
}

// DeleteTokenResponse is a response struct for DeleteToken endpoint.
type DeleteTokenResponse struct {
	Err error
}

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

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

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

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

			return DeleteTokenResponse{Err: err}, err
		}

		return DeleteTokenResponse{}, nil
	}
}

// GetTokenRequest is a request struct for GetToken endpoint.
type GetTokenRequest struct {
	Id string
}

// GetTokenResponse is a response struct for GetToken endpoint.
type GetTokenResponse struct {
	Token token.Token
	Err   error
}

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

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

		token, err := service.GetToken(ctx, req.Id)

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

			return GetTokenResponse{
				Err:   err,
				Token: token,
			}, err
		}

		return GetTokenResponse{Token: token}, nil
	}
}

// ListTokensRequest is a request struct for ListTokens endpoint.
type ListTokensRequest struct{}

// ListTokensResponse is a response struct for ListTokens endpoint.
type ListTokensResponse struct {
	Tokens []token.Token
	Err    error
}

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

// MakeListTokensEndpoint returns an endpoint for the matching method of the underlying service.
func MakeListTokensEndpoint(service token.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		tokens, err := service.ListTokens(ctx)

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

			return ListTokensResponse{
				Err:    err,
				Tokens: tokens,
			}, err
		}

		return ListTokensResponse{Tokens: tokens}, nil
	}
}
