// Code generated by go-swagger; DO NOT EDIT.

package restapi

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/loads"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/middleware"
	"github.com/go-openapi/runtime/security"

	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/bundle_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/cycle_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/plan_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/price_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/sku_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/status_management"
	"github.com/Cyclops-Labs/cyclops-4-hpc.git/services/plan-manager/restapi/operations/trigger_management"
)

type contextKey string

const AuthKey contextKey = "Auth"

//go:generate mockery -name BundleManagementAPI -inpkg

/* BundleManagementAPI  */
type BundleManagementAPI interface {
	/* CreateSkuBundle create SKU bundle */
	CreateSkuBundle(ctx context.Context, params bundle_management.CreateSkuBundleParams) middleware.Responder

	/* GetSkuBundle Get specific sku bundle */
	GetSkuBundle(ctx context.Context, params bundle_management.GetSkuBundleParams) middleware.Responder

	/* GetSkuBundleByName Get specific sku bundle */
	GetSkuBundleByName(ctx context.Context, params bundle_management.GetSkuBundleByNameParams) middleware.Responder

	/* ListSkuBundles list SKU Bundles */
	ListSkuBundles(ctx context.Context, params bundle_management.ListSkuBundlesParams) middleware.Responder

	/* UpdateSkuBundle Update specific sku bundle */
	UpdateSkuBundle(ctx context.Context, params bundle_management.UpdateSkuBundleParams) middleware.Responder
}

//go:generate mockery -name CycleManagementAPI -inpkg

/* CycleManagementAPI  */
type CycleManagementAPI interface {
	/* CreateCycle Create a plan */
	CreateCycle(ctx context.Context, params cycle_management.CreateCycleParams) middleware.Responder

	/* GetCycle Get specific cycle */
	GetCycle(ctx context.Context, params cycle_management.GetCycleParams) middleware.Responder

	/* ListCycles List all cycles */
	ListCycles(ctx context.Context, params cycle_management.ListCyclesParams) middleware.Responder

	/* UpdateCycle Update specific cycle */
	UpdateCycle(ctx context.Context, params cycle_management.UpdateCycleParams) middleware.Responder
}

//go:generate mockery -name PlanManagementAPI -inpkg

/* PlanManagementAPI  */
type PlanManagementAPI interface {
	/* CreatePlan Create a plan */
	CreatePlan(ctx context.Context, params plan_management.CreatePlanParams) middleware.Responder

	/* GetCompletePlan Get complete plan */
	GetCompletePlan(ctx context.Context, params plan_management.GetCompletePlanParams) middleware.Responder

	/* GetPlan Get specific plan */
	GetPlan(ctx context.Context, params plan_management.GetPlanParams) middleware.Responder

	/* ListCompletePlans Get full information relating to known plans */
	ListCompletePlans(ctx context.Context, params plan_management.ListCompletePlansParams) middleware.Responder

	/* ListPlans List all plans */
	ListPlans(ctx context.Context, params plan_management.ListPlansParams) middleware.Responder

	/* UpdatePlan Update specific plan */
	UpdatePlan(ctx context.Context, params plan_management.UpdatePlanParams) middleware.Responder
}

//go:generate mockery -name PriceManagementAPI -inpkg

/* PriceManagementAPI  */
type PriceManagementAPI interface {
	/* CreateSkuPrice create SKU price */
	CreateSkuPrice(ctx context.Context, params price_management.CreateSkuPriceParams) middleware.Responder

	/* GetSkuPrice Get specific sku price */
	GetSkuPrice(ctx context.Context, params price_management.GetSkuPriceParams) middleware.Responder

	/* ListSkuPrices list SKU Prices */
	ListSkuPrices(ctx context.Context, params price_management.ListSkuPricesParams) middleware.Responder

	/* UpdateSkuPrice Update specific sku price */
	UpdateSkuPrice(ctx context.Context, params price_management.UpdateSkuPriceParams) middleware.Responder
}

//go:generate mockery -name SkuManagementAPI -inpkg

/* SkuManagementAPI  */
type SkuManagementAPI interface {
	/* CreateSku create SKU */
	CreateSku(ctx context.Context, params sku_management.CreateSkuParams) middleware.Responder

	/* GetSku Get specific sku */
	GetSku(ctx context.Context, params sku_management.GetSkuParams) middleware.Responder

	/* ListSkus list SKUs */
	ListSkus(ctx context.Context, params sku_management.ListSkusParams) middleware.Responder

	/* UpdateSku Update specific sku */
	UpdateSku(ctx context.Context, params sku_management.UpdateSkuParams) middleware.Responder
}

//go:generate mockery -name StatusManagementAPI -inpkg

/* StatusManagementAPI  */
type StatusManagementAPI interface {
	/* GetStatus Basic status of the system */
	GetStatus(ctx context.Context, params status_management.GetStatusParams) middleware.Responder

	/* ShowStatus Basic status of the system */
	ShowStatus(ctx context.Context, params status_management.ShowStatusParams) middleware.Responder
}

//go:generate mockery -name TriggerManagementAPI -inpkg

/* TriggerManagementAPI  */
type TriggerManagementAPI interface {
	/* ExecSample Sample task trigger */
	ExecSample(ctx context.Context, params trigger_management.ExecSampleParams) middleware.Responder
}

// Config is configuration for Handler
type Config struct {
	BundleManagementAPI
	CycleManagementAPI
	PlanManagementAPI
	PriceManagementAPI
	SkuManagementAPI
	StatusManagementAPI
	TriggerManagementAPI
	Logger func(string, ...interface{})
	// InnerMiddleware is for the handler executors. These do not apply to the swagger.json document.
	// The middleware executes after routing but before authentication, binding and validation
	InnerMiddleware func(http.Handler) http.Handler

	// Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions
	// and the principal was stored in the context in the "AuthKey" context value.
	Authorizer func(*http.Request) error

	// AuthAPIKeyHeader Applies when the "X-API-KEY" header is set
	AuthAPIKeyHeader func(token string) (interface{}, error)

	// AuthAPIKeyParam Applies when the "api_key" query is set
	AuthAPIKeyParam func(token string) (interface{}, error)

	// AuthKeycloak For OAuth2 authentication
	AuthKeycloak func(token string, scopes []string) (interface{}, error)
	// Authenticator to use for all APIKey authentication
	APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator
	// Authenticator to use for all Bearer authentication
	BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
	// Authenticator to use for all Basic authentication
	BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
}

// Handler returns an http.Handler given the handler configuration
// It mounts all the business logic implementers in the right routing.
func Handler(c Config) (http.Handler, error) {
	h, _, err := HandlerAPI(c)
	return h, err
}

// HandlerAPI returns an http.Handler given the handler configuration
// and the corresponding *PlanManagerManagementAPI instance.
// It mounts all the business logic implementers in the right routing.
func HandlerAPI(c Config) (http.Handler, *operations.PlanManagerManagementAPIAPI, error) {
	spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "")
	if err != nil {
		return nil, nil, fmt.Errorf("analyze swagger: %v", err)
	}
	api := operations.NewPlanManagerManagementAPIAPI(spec)
	api.ServeError = errors.ServeError
	api.Logger = c.Logger

	if c.APIKeyAuthenticator != nil {
		api.APIKeyAuthenticator = c.APIKeyAuthenticator
	}
	if c.BasicAuthenticator != nil {
		api.BasicAuthenticator = c.BasicAuthenticator
	}
	if c.BearerAuthenticator != nil {
		api.BearerAuthenticator = c.BearerAuthenticator
	}

	api.JSONConsumer = runtime.JSONConsumer()
	api.JSONProducer = runtime.JSONProducer()
	api.APIKeyHeaderAuth = func(token string) (interface{}, error) {
		if c.AuthAPIKeyHeader == nil {
			return token, nil
		}
		return c.AuthAPIKeyHeader(token)
	}

	api.APIKeyParamAuth = func(token string) (interface{}, error) {
		if c.AuthAPIKeyParam == nil {
			return token, nil
		}
		return c.AuthAPIKeyParam(token)
	}

	api.KeycloakAuth = func(token string, scopes []string) (interface{}, error) {
		if c.AuthKeycloak == nil {
			return token, nil
		}
		return c.AuthKeycloak(token, scopes)
	}
	api.APIAuthorizer = authorizer(c.Authorizer)
	api.CycleManagementCreateCycleHandler = cycle_management.CreateCycleHandlerFunc(func(params cycle_management.CreateCycleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.CycleManagementAPI.CreateCycle(ctx, params)
	})
	api.PlanManagementCreatePlanHandler = plan_management.CreatePlanHandlerFunc(func(params plan_management.CreatePlanParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.CreatePlan(ctx, params)
	})
	api.SkuManagementCreateSkuHandler = sku_management.CreateSkuHandlerFunc(func(params sku_management.CreateSkuParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.SkuManagementAPI.CreateSku(ctx, params)
	})
	api.BundleManagementCreateSkuBundleHandler = bundle_management.CreateSkuBundleHandlerFunc(func(params bundle_management.CreateSkuBundleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.BundleManagementAPI.CreateSkuBundle(ctx, params)
	})
	api.PriceManagementCreateSkuPriceHandler = price_management.CreateSkuPriceHandlerFunc(func(params price_management.CreateSkuPriceParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PriceManagementAPI.CreateSkuPrice(ctx, params)
	})
	api.TriggerManagementExecSampleHandler = trigger_management.ExecSampleHandlerFunc(func(params trigger_management.ExecSampleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.TriggerManagementAPI.ExecSample(ctx, params)
	})
	api.PlanManagementGetCompletePlanHandler = plan_management.GetCompletePlanHandlerFunc(func(params plan_management.GetCompletePlanParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.GetCompletePlan(ctx, params)
	})
	api.CycleManagementGetCycleHandler = cycle_management.GetCycleHandlerFunc(func(params cycle_management.GetCycleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.CycleManagementAPI.GetCycle(ctx, params)
	})
	api.PlanManagementGetPlanHandler = plan_management.GetPlanHandlerFunc(func(params plan_management.GetPlanParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.GetPlan(ctx, params)
	})
	api.SkuManagementGetSkuHandler = sku_management.GetSkuHandlerFunc(func(params sku_management.GetSkuParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.SkuManagementAPI.GetSku(ctx, params)
	})
	api.BundleManagementGetSkuBundleHandler = bundle_management.GetSkuBundleHandlerFunc(func(params bundle_management.GetSkuBundleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.BundleManagementAPI.GetSkuBundle(ctx, params)
	})
	api.BundleManagementGetSkuBundleByNameHandler = bundle_management.GetSkuBundleByNameHandlerFunc(func(params bundle_management.GetSkuBundleByNameParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.BundleManagementAPI.GetSkuBundleByName(ctx, params)
	})
	api.PriceManagementGetSkuPriceHandler = price_management.GetSkuPriceHandlerFunc(func(params price_management.GetSkuPriceParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PriceManagementAPI.GetSkuPrice(ctx, params)
	})
	api.StatusManagementGetStatusHandler = status_management.GetStatusHandlerFunc(func(params status_management.GetStatusParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.StatusManagementAPI.GetStatus(ctx, params)
	})
	api.PlanManagementListCompletePlansHandler = plan_management.ListCompletePlansHandlerFunc(func(params plan_management.ListCompletePlansParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.ListCompletePlans(ctx, params)
	})
	api.CycleManagementListCyclesHandler = cycle_management.ListCyclesHandlerFunc(func(params cycle_management.ListCyclesParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.CycleManagementAPI.ListCycles(ctx, params)
	})
	api.PlanManagementListPlansHandler = plan_management.ListPlansHandlerFunc(func(params plan_management.ListPlansParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.ListPlans(ctx, params)
	})
	api.BundleManagementListSkuBundlesHandler = bundle_management.ListSkuBundlesHandlerFunc(func(params bundle_management.ListSkuBundlesParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.BundleManagementAPI.ListSkuBundles(ctx, params)
	})
	api.PriceManagementListSkuPricesHandler = price_management.ListSkuPricesHandlerFunc(func(params price_management.ListSkuPricesParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PriceManagementAPI.ListSkuPrices(ctx, params)
	})
	api.SkuManagementListSkusHandler = sku_management.ListSkusHandlerFunc(func(params sku_management.ListSkusParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.SkuManagementAPI.ListSkus(ctx, params)
	})
	api.StatusManagementShowStatusHandler = status_management.ShowStatusHandlerFunc(func(params status_management.ShowStatusParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.StatusManagementAPI.ShowStatus(ctx, params)
	})
	api.CycleManagementUpdateCycleHandler = cycle_management.UpdateCycleHandlerFunc(func(params cycle_management.UpdateCycleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.CycleManagementAPI.UpdateCycle(ctx, params)
	})
	api.PlanManagementUpdatePlanHandler = plan_management.UpdatePlanHandlerFunc(func(params plan_management.UpdatePlanParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PlanManagementAPI.UpdatePlan(ctx, params)
	})
	api.SkuManagementUpdateSkuHandler = sku_management.UpdateSkuHandlerFunc(func(params sku_management.UpdateSkuParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.SkuManagementAPI.UpdateSku(ctx, params)
	})
	api.BundleManagementUpdateSkuBundleHandler = bundle_management.UpdateSkuBundleHandlerFunc(func(params bundle_management.UpdateSkuBundleParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.BundleManagementAPI.UpdateSkuBundle(ctx, params)
	})
	api.PriceManagementUpdateSkuPriceHandler = price_management.UpdateSkuPriceHandlerFunc(func(params price_management.UpdateSkuPriceParams, principal interface{}) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		ctx = storeAuth(ctx, principal)
		return c.PriceManagementAPI.UpdateSkuPrice(ctx, params)
	})
	api.ServerShutdown = func() {}
	return api.Serve(c.InnerMiddleware), api, nil
}

// swaggerCopy copies the swagger json to prevent data races in runtime
func swaggerCopy(orig json.RawMessage) json.RawMessage {
	c := make(json.RawMessage, len(orig))
	copy(c, orig)
	return c
}

// authorizer is a helper function to implement the runtime.Authorizer interface.
type authorizer func(*http.Request) error

func (a authorizer) Authorize(req *http.Request, principal interface{}) error {
	if a == nil {
		return nil
	}
	ctx := storeAuth(req.Context(), principal)
	return a(req.WithContext(ctx))
}

func storeAuth(ctx context.Context, principal interface{}) context.Context {
	return context.WithValue(ctx, AuthKey, principal)
}
