//
// This file is generated by grafana-app-sdk
// DO NOT EDIT
//

package manifestdata

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/grafana/grafana-app-sdk/app"
	"github.com/grafana/grafana-app-sdk/resource"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/kube-openapi/pkg/spec3"
	"k8s.io/kube-openapi/pkg/validation/spec"

	customkindv0_0 "codegen-tests/pkg/generated/customkind/v0_0"
	customkindv1_0 "codegen-tests/pkg/generated/customkind/v1_0"
)

var (
	rawSchemaCustomKindv0_0     = []byte(`{"CustomKind":{"properties":{"spec":{"$ref":"#/components/schemas/spec"},"status":{"$ref":"#/components/schemas/status"}},"required":["spec"]},"OperatorState":{"additionalProperties":false,"properties":{"descriptiveState":{"description":"descriptiveState is an optional more descriptive state field which has no requirements on format","type":"string"},"details":{"additionalProperties":true,"description":"details contains any extra information that is operator-specific","type":"object"},"lastEvaluation":{"description":"lastEvaluation is the ResourceVersion last evaluated","type":"string"},"state":{"description":"state describes the state of the lastEvaluation.\nIt is limited to three possible states for machine evaluation.","enum":["success","in_progress","failed"],"type":"string"}},"required":["lastEvaluation","state"],"type":"object"},"spec":{"additionalProperties":false,"properties":{"deprecatedField":{"type":"string"},"field1":{"type":"string"}},"required":["field1","deprecatedField"],"type":"object"},"status":{"additionalProperties":false,"properties":{"additionalFields":{"additionalProperties":true,"description":"additionalFields is reserved for future use","type":"object"},"operatorStates":{"additionalProperties":{"$ref":"#/components/schemas/OperatorState"},"description":"operatorStates is a map of operator ID to operator state evaluations.\nAny operator which consumes this kind SHOULD add its state evaluation information to this field.","type":"object"}},"type":"object"}}`)
	versionSchemaCustomKindv0_0 app.VersionSchema
	_                           = json.Unmarshal(rawSchemaCustomKindv0_0, &versionSchemaCustomKindv0_0)
	rawSchemaCustomKindv1_0     = []byte(`{"CustomKind":{"properties":{"spec":{"$ref":"#/components/schemas/spec"},"status":{"$ref":"#/components/schemas/status"}},"required":["spec"]},"InnerObject1":{"additionalProperties":false,"properties":{"innerField1":{"type":"string"},"innerField2":{"items":{"type":"string"},"type":"array"},"innerField3":{"items":{"$ref":"#/components/schemas/InnerObject2"},"type":"array"},"innerField4":{"items":{"additionalProperties":true,"type":"object"},"type":"array"}},"required":["innerField1","innerField2","innerField3","innerField4"],"type":"object"},"InnerObject2":{"additionalProperties":false,"properties":{"details":{"additionalProperties":true,"type":"object"},"name":{"type":"string"}},"required":["name","details"],"type":"object"},"OperatorState":{"additionalProperties":false,"properties":{"descriptiveState":{"description":"descriptiveState is an optional more descriptive state field which has no requirements on format","type":"string"},"details":{"additionalProperties":true,"description":"details contains any extra information that is operator-specific","type":"object"},"lastEvaluation":{"description":"lastEvaluation is the ResourceVersion last evaluated","type":"string"},"state":{"description":"state describes the state of the lastEvaluation.\nIt is limited to three possible states for machine evaluation.","enum":["success","in_progress","failed"],"type":"string"}},"required":["lastEvaluation","state"],"type":"object"},"RecursiveList":{"additionalProperties":false,"properties":{"next":{"$ref":"#/components/schemas/RecursiveList"},"val":{"type":"string"}},"required":["val"],"type":"object"},"Type1":{"additionalProperties":false,"properties":{"group":{"type":"string"},"options":{"items":{"type":"string"},"type":"array"}},"required":["group"],"type":"object"},"Type2":{"additionalProperties":false,"properties":{"details":{"additionalProperties":true,"type":"object"},"group":{"type":"string"}},"required":["group","details"],"type":"object"},"UnionType":{"oneOf":[{"$ref":"#/components/schemas/Type1"},{"$ref":"#/components/schemas/Type2"}]},"spec":{"additionalProperties":false,"properties":{"boolField":{"default":false,"type":"boolean"},"enum":{"default":"default","enum":["val1","val2","val3","val4","default"],"type":"string"},"exclusiveInt":{"exclusiveMaximum":true,"exclusiveMinimum":true,"maximum":100,"minimum":10,"type":"integer"},"field1":{"type":"string"},"floatField":{"type":"number"},"i32":{"maximum":123456,"type":"integer"},"i64":{"minimum":123456,"type":"integer"},"inner":{"$ref":"#/components/schemas/InnerObject1"},"linkedList":{"$ref":"#/components/schemas/RecursiveList"},"map":{"additionalProperties":{"$ref":"#/components/schemas/Type2"},"type":"object"},"timestamp":{"format":"date-time","type":"string"},"union":{"$ref":"#/components/schemas/UnionType"}},"required":["field1","inner","union","map","timestamp","enum","i32","i64","boolField","floatField","linkedList","exclusiveInt"],"type":"object"},"status":{"additionalProperties":{},"properties":{"additionalFields":{"additionalProperties":true,"description":"additionalFields is reserved for future use","type":"object"},"operatorStates":{"additionalProperties":{"$ref":"#/components/schemas/OperatorState"},"description":"operatorStates is a map of operator ID to operator state evaluations.\nAny operator which consumes this kind SHOULD add its state evaluation information to this field.","type":"object"},"statusField1":{"type":"string"}},"required":["statusField1"],"type":"object"}}`)
	versionSchemaCustomKindv1_0 app.VersionSchema
	_                           = json.Unmarshal(rawSchemaCustomKindv1_0, &versionSchemaCustomKindv1_0)
)

var appManifestData = app.ManifestData{
	AppName:          "custom-app",
	AppDisplayName:   "custom-app",
	Group:            "customapp.ext.grafana.app",
	PreferredVersion: "v1-0",
	Versions: []app.ManifestVersion{
		{
			Name:   "v0-0",
			Served: true,
			Kinds: []app.ManifestVersionKind{
				{
					Kind:       "CustomKind",
					Plural:     "CustomKinds",
					Scope:      "Namespaced",
					Conversion: false,
					Schema:     &versionSchemaCustomKindv0_0,
				},
			},
			Routes: app.ManifestVersionRoutes{
				Namespaced: map[string]spec3.PathProps{},
				Cluster:    map[string]spec3.PathProps{},
				Schemas:    map[string]spec.Schema{},
			},
		},

		{
			Name:   "v1-0",
			Served: true,
			Kinds: []app.ManifestVersionKind{
				{
					Kind:       "CustomKind",
					Plural:     "CustomKinds",
					Scope:      "Namespaced",
					Conversion: false,
					Schema:     &versionSchemaCustomKindv1_0,
				},
			},
			Routes: app.ManifestVersionRoutes{
				Namespaced: map[string]spec3.PathProps{},
				Cluster:    map[string]spec3.PathProps{},
				Schemas:    map[string]spec.Schema{},
			},
		},
	},
	Roles: map[string]app.ManifestRole{
		"custom-app:admin": {
			Title:       "custom-app Admin",
			Description: "Allows all actions on CustomKinds",
			Kinds: []app.ManifestRoleKind{
				{
					Kind:          "CustomKind",
					PermissionSet: strPtr("admin"),
				},
				{
					Kind:          "CustomKind/status",
					PermissionSet: strPtr("admin"),
				},
			},
			Routes: []string{},
		},
		"custom-app:editor": {
			Title:       "custom-app Editor",
			Description: "Create, Read, Update, and Delete CustomKinds",
			Kinds: []app.ManifestRoleKind{
				{
					Kind:          "CustomKind",
					PermissionSet: strPtr("editor"),
				},
			},
			Routes: []string{},
		},
		"custom-app:reader": {
			Title:       "custom-app Reader",
			Description: "Read CustomKinds",
			Kinds: []app.ManifestRoleKind{
				{
					Kind:          "CustomKind",
					PermissionSet: strPtr("viewer"),
				},
			},
			Routes: []string{},
		},
	},
	RoleBindings: &app.ManifestRoleBindings{
		Viewer: []string{
			"custom-app:reader",
		},
		Editor: []string{
			"custom-app:editor",
		},
		Admin: []string{
			"custom-app:admin",
		},
	},
}

func LocalManifest() app.Manifest {
	return app.NewEmbeddedManifest(appManifestData)
}

func RemoteManifest() app.Manifest {
	return app.NewAPIServerManifest("custom-app")
}

var kindVersionToGoType = map[string]resource.Kind{
	"CustomKind/v0-0": customkindv0_0.Kind(),
	"CustomKind/v1-0": customkindv1_0.Kind(),
}

// ManifestGoTypeAssociator returns the associated resource.Kind instance for a given Kind and Version, if one exists.
// If there is no association for the provided Kind and Version, exists will return false.
func ManifestGoTypeAssociator(kind, version string) (goType resource.Kind, exists bool) {
	goType, exists = kindVersionToGoType[fmt.Sprintf("%s/%s", kind, version)]
	return goType, exists
}

var customRouteToGoResponseType = map[string]any{}

// ManifestCustomRouteResponsesAssociator returns the associated response go type for a given kind, version, custom route path, and method, if one exists.
// kind may be empty for custom routes which are not kind subroutes. Leading slashes are removed from subroute paths.
// If there is no association for the provided kind, version, custom route path, and method, exists will return false.
// Resource routes (those without a kind) should prefix their route with "<namespace>/" if the route is namespaced (otherwise the route is assumed to be cluster-scope)
func ManifestCustomRouteResponsesAssociator(kind, version, path, verb string) (goType any, exists bool) {
	if len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}
	goType, exists = customRouteToGoResponseType[fmt.Sprintf("%s|%s|%s|%s", version, kind, path, strings.ToUpper(verb))]
	return goType, exists
}

var customRouteToGoParamsType = map[string]runtime.Object{}

func ManifestCustomRouteQueryAssociator(kind, version, path, verb string) (goType runtime.Object, exists bool) {
	if len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}
	goType, exists = customRouteToGoParamsType[fmt.Sprintf("%s|%s|%s|%s", version, kind, path, strings.ToUpper(verb))]
	return goType, exists
}

var customRouteToGoRequestBodyType = map[string]any{}

func ManifestCustomRouteRequestBodyAssociator(kind, version, path, verb string) (goType any, exists bool) {
	if len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}
	goType, exists = customRouteToGoRequestBodyType[fmt.Sprintf("%s|%s|%s|%s", version, kind, path, strings.ToUpper(verb))]
	return goType, exists
}

type GoTypeAssociator struct{}

func NewGoTypeAssociator() *GoTypeAssociator {
	return &GoTypeAssociator{}
}

func (g *GoTypeAssociator) KindToGoType(kind, version string) (goType resource.Kind, exists bool) {
	return ManifestGoTypeAssociator(kind, version)
}
func (g *GoTypeAssociator) CustomRouteReturnGoType(kind, version, path, verb string) (goType any, exists bool) {
	return ManifestCustomRouteResponsesAssociator(kind, version, path, verb)
}
func (g *GoTypeAssociator) CustomRouteQueryGoType(kind, version, path, verb string) (goType runtime.Object, exists bool) {
	return ManifestCustomRouteQueryAssociator(kind, version, path, verb)
}
func (g *GoTypeAssociator) CustomRouteRequestBodyGoType(kind, version, path, verb string) (goType any, exists bool) {
	return ManifestCustomRouteRequestBodyAssociator(kind, version, path, verb)
}
func strPtr(s string) *string {
	return &s
}
