// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.15.0 DO NOT EDIT.
package api

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

	"github.com/labstack/echo/v4"
	strictecho "github.com/oapi-codegen/runtime/strictmiddleware/echo"
)

// AggregatedTransactions Aggregated transactions data
type AggregatedTransactions struct {
	// Daily Aggregated transactions data for the last day
	Daily []DataPoint `json:"daily"`

	// Hourly Aggregated transactions data for the last hour
	Hourly []DataPoint `json:"hourly"`

	// Monthly Aggregated transactions data for the last month
	Monthly []DataPoint `json:"monthly"`
}

// DataPoint Data point
type DataPoint struct {
	// ContentType content type of the data point
	ContentType string `json:"contentType"`

	// Label time of the data point formatted as RFC3339
	Label string `json:"label"`

	// Timestamp time of the data point formatted as unix timestamp
	Timestamp int `json:"timestamp"`

	// Value number of transactions between the given timestamp and the next timestamp
	Value int `json:"value"`
}

// Network network and connection diagnostics
type Network struct {
	Connections struct {
		// ConnectedPeersCount number of peers connected
		ConnectedPeersCount int `json:"connected_peers_count"`

		// PeerId identifier generated at startup of the node
		PeerId string `json:"peer_id"`
	} `json:"connections"`

	// NodeDid DID connected to the node.
	NodeDid *string `json:"node_did,omitempty"`

	// State Key numbers of the DAG
	State struct {
		// DagLcHigh highest LC value
		DagLcHigh int `json:"dag_lc_high"`

		// DagXor XOR value of all transaction refs
		DagXor string `json:"dag_xor"`

		// FailedEvents number of failed internal events
		FailedEvents int `json:"failed_events"`

		// StoredDatabaseSizeBytes size of the DB in bytes
		StoredDatabaseSizeBytes float32 `json:"stored_database_size_bytes"`

		// TransactionCount number of transactions on the network
		TransactionCount float32 `json:"transaction_count"`
	} `json:"state"`
}

// Status characteristics of running process
type Status struct {
	// GitCommit hash of latest commit in github used to build the current binary
	GitCommit string `json:"git_commit"`

	// OsArch System architecture (arm,amd64, etc)
	OsArch string `json:"os_arch"`

	// SoftwareVersion Github tag or branch
	SoftwareVersion string `json:"software_version"`

	// Uptime Nanoseconds of uptime
	Uptime float32 `json:"uptime"`
}

// TransactionCounts Transaction counts
type TransactionCounts struct {
	// RootCount number of root DIDs in the network
	RootCount int `json:"root_count"`

	// TransactionsPerRoot number of transactions per root DID.
	TransactionsPerRoot []TransactionsPerRoot `json:"transactions_per_root"`
}

// TransactionsPerRoot number of transactions per root DID.
type TransactionsPerRoot struct {
	// Count number of transactions for the root DID
	Count int `json:"count"`

	// Did root DID
	Did string `json:"did"`
}

// VCR key numbers on credentials
type VCR struct {
	// CredentialCount total number of observed credentials
	CredentialCount int `json:"credential_count"`

	// Issuer numbers on issued credentials
	Issuer struct {
		// IssuedCredentialsCount number of issued credentials from this node
		IssuedCredentialsCount int `json:"issued_credentials_count"`

		// RevokedCredentialsCount number of revoked credentials from this node
		RevokedCredentialsCount int `json:"revoked_credentials_count"`
	} `json:"issuer"`
	Verifier struct {
		// RevocationsCount total number of revocations in the network
		RevocationsCount int `json:"revocations_count"`
	} `json:"verifier"`
}

// VDR key numbers on DID Documents
type VDR struct {
	// ConflictedDidDocuments numbers on conflicted DID documents
	ConflictedDidDocuments struct {
		// OwnedCount number of conflicted documents that are under this node's control
		OwnedCount int `json:"owned_count"`

		// TotalCount total number of conflicted documents
		TotalCount int `json:"total_count"`
	} `json:"conflicted_did_documents"`

	// DidDocumentsCount total number of DID Documents
	DidDocumentsCount int `json:"did_documents_count"`
}

// ServerInterface represents all server handlers.
type ServerInterface interface {
	// More elaborate health check to conform the app is (probably) functioning correctly
	// (GET /health)
	CheckHealth(ctx echo.Context) error
	// Returns the node key diagnostics
	// (GET /web/diagnostics)
	Diagnostics(ctx echo.Context) error
	// Returns the network as a graph model
	// (GET /web/network_topology)
	NetworkTopology(ctx echo.Context) error
	// Returns the transactions aggregated by time
	// (GET /web/transactions/aggregated)
	AggregatedTransactions(ctx echo.Context) error
	// Return the number of transactions per node and total known nodes
	// (GET /web/transactions/counts)
	TransactionCounts(ctx echo.Context) error
}

// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
	Handler ServerInterface
}

// CheckHealth converts echo context to params.
func (w *ServerInterfaceWrapper) CheckHealth(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.CheckHealth(ctx)
	return err
}

// Diagnostics converts echo context to params.
func (w *ServerInterfaceWrapper) Diagnostics(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.Diagnostics(ctx)
	return err
}

// NetworkTopology converts echo context to params.
func (w *ServerInterfaceWrapper) NetworkTopology(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.NetworkTopology(ctx)
	return err
}

// AggregatedTransactions converts echo context to params.
func (w *ServerInterfaceWrapper) AggregatedTransactions(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.AggregatedTransactions(ctx)
	return err
}

// TransactionCounts converts echo context to params.
func (w *ServerInterfaceWrapper) TransactionCounts(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.TransactionCounts(ctx)
	return err
}

// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
	CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
	RegisterHandlersWithBaseURL(router, si, "")
}

// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {

	wrapper := ServerInterfaceWrapper{
		Handler: si,
	}

	router.GET(baseURL+"/health", wrapper.CheckHealth)
	router.GET(baseURL+"/web/diagnostics", wrapper.Diagnostics)
	router.GET(baseURL+"/web/network_topology", wrapper.NetworkTopology)
	router.GET(baseURL+"/web/transactions/aggregated", wrapper.AggregatedTransactions)
	router.GET(baseURL+"/web/transactions/counts", wrapper.TransactionCounts)

}

type CheckHealthRequestObject struct {
}

type CheckHealthResponseObject interface {
	VisitCheckHealthResponse(w http.ResponseWriter) error
}

type CheckHealth200JSONResponse CheckHealthResponse

func (response CheckHealth200JSONResponse) VisitCheckHealthResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	return json.NewEncoder(w).Encode(response)
}

type CheckHealth503JSONResponse CheckHealthResponse

func (response CheckHealth503JSONResponse) VisitCheckHealthResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(503)

	return json.NewEncoder(w).Encode(response)
}

type DiagnosticsRequestObject struct {
}

type DiagnosticsResponseObject interface {
	VisitDiagnosticsResponse(w http.ResponseWriter) error
}

type Diagnostics200JSONResponse Diagnostics

func (response Diagnostics200JSONResponse) VisitDiagnosticsResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	return json.NewEncoder(w).Encode(response)
}

type NetworkTopologyRequestObject struct {
}

type NetworkTopologyResponseObject interface {
	VisitNetworkTopologyResponse(w http.ResponseWriter) error
}

type NetworkTopology200JSONResponse NetworkTopology

func (response NetworkTopology200JSONResponse) VisitNetworkTopologyResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	return json.NewEncoder(w).Encode(response)
}

type AggregatedTransactionsRequestObject struct {
}

type AggregatedTransactionsResponseObject interface {
	VisitAggregatedTransactionsResponse(w http.ResponseWriter) error
}

type AggregatedTransactions200JSONResponse AggregatedTransactions

func (response AggregatedTransactions200JSONResponse) VisitAggregatedTransactionsResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	return json.NewEncoder(w).Encode(response)
}

type TransactionCountsRequestObject struct {
}

type TransactionCountsResponseObject interface {
	VisitTransactionCountsResponse(w http.ResponseWriter) error
}

type TransactionCounts200JSONResponse TransactionCounts

func (response TransactionCounts200JSONResponse) VisitTransactionCountsResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	return json.NewEncoder(w).Encode(response)
}

// StrictServerInterface represents all server handlers.
type StrictServerInterface interface {
	// More elaborate health check to conform the app is (probably) functioning correctly
	// (GET /health)
	CheckHealth(ctx context.Context, request CheckHealthRequestObject) (CheckHealthResponseObject, error)
	// Returns the node key diagnostics
	// (GET /web/diagnostics)
	Diagnostics(ctx context.Context, request DiagnosticsRequestObject) (DiagnosticsResponseObject, error)
	// Returns the network as a graph model
	// (GET /web/network_topology)
	NetworkTopology(ctx context.Context, request NetworkTopologyRequestObject) (NetworkTopologyResponseObject, error)
	// Returns the transactions aggregated by time
	// (GET /web/transactions/aggregated)
	AggregatedTransactions(ctx context.Context, request AggregatedTransactionsRequestObject) (AggregatedTransactionsResponseObject, error)
	// Return the number of transactions per node and total known nodes
	// (GET /web/transactions/counts)
	TransactionCounts(ctx context.Context, request TransactionCountsRequestObject) (TransactionCountsResponseObject, error)
}

type StrictHandlerFunc = strictecho.StrictEchoHandlerFunc
type StrictMiddlewareFunc = strictecho.StrictEchoMiddlewareFunc

func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
	return &strictHandler{ssi: ssi, middlewares: middlewares}
}

type strictHandler struct {
	ssi         StrictServerInterface
	middlewares []StrictMiddlewareFunc
}

// CheckHealth operation middleware
func (sh *strictHandler) CheckHealth(ctx echo.Context) error {
	var request CheckHealthRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.CheckHealth(ctx.Request().Context(), request.(CheckHealthRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "CheckHealth")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(CheckHealthResponseObject); ok {
		return validResponse.VisitCheckHealthResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}

// Diagnostics operation middleware
func (sh *strictHandler) Diagnostics(ctx echo.Context) error {
	var request DiagnosticsRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.Diagnostics(ctx.Request().Context(), request.(DiagnosticsRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "Diagnostics")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(DiagnosticsResponseObject); ok {
		return validResponse.VisitDiagnosticsResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}

// NetworkTopology operation middleware
func (sh *strictHandler) NetworkTopology(ctx echo.Context) error {
	var request NetworkTopologyRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.NetworkTopology(ctx.Request().Context(), request.(NetworkTopologyRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "NetworkTopology")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(NetworkTopologyResponseObject); ok {
		return validResponse.VisitNetworkTopologyResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}

// AggregatedTransactions operation middleware
func (sh *strictHandler) AggregatedTransactions(ctx echo.Context) error {
	var request AggregatedTransactionsRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.AggregatedTransactions(ctx.Request().Context(), request.(AggregatedTransactionsRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "AggregatedTransactions")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(AggregatedTransactionsResponseObject); ok {
		return validResponse.VisitAggregatedTransactionsResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}

// TransactionCounts operation middleware
func (sh *strictHandler) TransactionCounts(ctx echo.Context) error {
	var request TransactionCountsRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.TransactionCounts(ctx.Request().Context(), request.(TransactionCountsRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "TransactionCounts")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(TransactionCountsResponseObject); ok {
		return validResponse.VisitTransactionCountsResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}
