// Package component provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.1 DO NOT EDIT.
package component

import (
	"encoding/json"
	"fmt"

	core "github.com/meshery/schemas/models/core"
	capabilityv1beta1 "github.com/meshery/schemas/models/v1beta1/capability"
	modelv1beta1 "github.com/meshery/schemas/models/v1beta1/model"
)

// Defines values for ComponentDefinitionFormat.
const (
	CUE  ComponentDefinitionFormat = "CUE"
	JSON ComponentDefinitionFormat = "JSON"
)

// Defines values for ComponentDefinitionStatus.
const (
	Duplicate ComponentDefinitionStatus = "duplicate"
	Enabled   ComponentDefinitionStatus = "enabled"
	Ignored   ComponentDefinitionStatus = "ignored"
	Open      ComponentDefinitionStatus = "open"
	Resolved  ComponentDefinitionStatus = "resolved"
)

// Component data related to the third party capability that Component Defintion wraps , this is herematicaly sealed an
type Component struct {
	// Version Version of the component produced by the registrant. Example: APIVersion of a Kubernetes Pod.
	Version string `json:"version" yaml:"version"`

	// Kind The unique identifier (name) assigned by the registrant to this component. Example: A Kubernetes Pod is of kind 'Pod'.
	Kind string `json:"kind" yaml:"kind"`

	// Schema JSON schema of the object as defined by the registrant.
	Schema string `json:"schema" yaml:"schema"`
}

// ComponentDefinition Components are reusable building blocks for depicting capabilities defined within models. Learn more at https://docs.meshery.io/concepts/components
type ComponentDefinition struct {
	// Id A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	ID core.Uuid `json:"id" yaml:"id"`

	// SchemaVersion API version of the object, optionally prefixed with an API group (e.g. "group.example.io/v1beta1" or bare "v1beta1").
	SchemaVersion core.VersionString `json:"schemaVersion" yaml:"schemaVersion"`

	// Version A valid semantic version string between 5 and 100 characters. The pattern allows for a major.minor.patch version followed by an optional pre-release tag like '-alpha' or '-beta.2' and an optional build metadata tag like '+build.1'.
	Version core.SemverString `json:"version" yaml:"version"`

	// DisplayName A string starting with an alphanumeric character. Spaces and hyphens allowed.
	DisplayName core.InputString `json:"displayName" yaml:"displayName"`

	// Description A written representation of the purpose and characteristics of the component.
	Description string `json:"description" yaml:"description"`

	// Format Format specifies the format used in the `component.schema` field. JSON is the default.
	Format ComponentDefinitionFormat `json:"format" yaml:"format"`

	// Model Meshery Models serve as a portable unit of packaging to define managed entities, their relationships, and capabilities.
	Model *modelv1beta1.ModelDefinition `gorm:"foreignKey:ModelId;references:ID" json:"model" yaml:"model"`

	// ModelReference Reference to the specific registered model to which the component belongs and from which model version, category, and other properties may be referenced. Learn more at https://docs.meshery.io/concepts/models
	ModelReference modelv1beta1.ModelReference `gorm:"-" json:"modelReference" yaml:"modelReference"`

	// Styles Visualization styles for a component
	Styles *core.ComponentStyles `gorm:"type:bytes;serializer:json" json:"styles" yaml:"styles"`

	// Capabilities Meshery manages components in accordance with their specific capabilities. This field explicitly identifies those capabilities largely by what actions a given component supports; e.g. metric-scrape, sub-interface, and so on. This field is extensible. ComponentDefinitions may define a broad array of capabilities, which are in-turn dynamically interpretted by Meshery for full lifecycle management.
	Capabilities *[]capabilityv1beta1.Capability `gorm:"type:bytes;serializer:json" json:"capabilities" yaml:"capabilities"`

	// Status Status of component, including:
	// - duplicate: this component is a duplicate of another. The component that is to be the canonical reference and that is duplicated by other components should not be assigned the 'duplicate' status.
	// - maintenance: model is unavailable for a period of time.
	// - enabled: model is available for use for all users of this Meshery Server.
	// - ignored: model is unavailable for use for all users of this Meshery Server.
	Status *ComponentDefinitionStatus `json:"status" yaml:"status"`

	// Metadata Metadata contains additional information associated with the component.
	Metadata ComponentDefinition_Metadata `gorm:"type:bytes;serializer:json" json:"metadata" yaml:"metadata"`

	// Configuration The configuration of the component. The configuration is based on the schema defined within the component definition(component.schema).
	Configuration map[string]interface{} `gorm:"type:bytes;serializer:json" json:"configuration" yaml:"configuration"`

	// Component data related to the third party capability that Component Defintion wraps , this is herematicaly sealed an
	Component Component `gorm:"type:bytes;serializer:json" json:"component" yaml:"component"`

	// CreatedAt Timestamp when the resource was created.
	CreatedAt core.CreatedAt `db:"created_at" json:"created_at,omitempty" yaml:"created_at,omitempty"`

	// UpdatedAt Timestamp when the resource was updated.
	UpdatedAt core.UpdatedAt `db:"updated_at" json:"updated_at,omitempty" yaml:"updated_at,omitempty"`

	// ModelId A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	ModelId *core.Uuid `gorm:"index:idx_component_definition_dbs_model_id,column:model_id" json:"-" yaml:"-"`
}

// ComponentDefinitionFormat Format specifies the format used in the `component.schema` field. JSON is the default.
type ComponentDefinitionFormat string

// ComponentDefinitionStatus Status of component, including:
// - duplicate: this component is a duplicate of another. The component that is to be the canonical reference and that is duplicated by other components should not be assigned the 'duplicate' status.
// - maintenance: model is unavailable for a period of time.
// - enabled: model is available for use for all users of this Meshery Server.
// - ignored: model is unavailable for use for all users of this Meshery Server.
type ComponentDefinitionStatus string

// ComponentDefinition_Metadata Metadata contains additional information associated with the component.
type ComponentDefinition_Metadata struct {
	// Genealogy Genealogy represents the various representational states of the component.
	Genealogy string `json:"genealogy" yaml:"genealogy"`

	// IsAnnotation Identifies whether the component is semantically meaningful or not; identifies whether the component should be treated as deployable entity or is for purposes of logical representation.
	IsAnnotation bool `json:"isAnnotation" yaml:"isAnnotation"`

	// IsNamespaced Identifies whether the component is scoped to namespace or clsuter wide.
	IsNamespaced bool `json:"isNamespaced" yaml:"isNamespaced"`

	// Published 'published' controls whether the component should be registered in Meshery Registry. When the same 'published' property in Models, is set to 'false', the Model property takes precedence with all Entities in the Model not being registered.
	Published bool `json:"published" yaml:"published"`

	// InstanceDetails InstanceDetails contains information about the instance of the component.
	InstanceDetails map[string]interface{} `json:"instanceDetails" yaml:"instanceDetails"`

	// ConfigurationUISchema Defines the UI schema for rendering the component's configuration. For more details, visit: https://rjsf-team.github.io/react-jsonschema-form/docs/api-reference/uiSchema/ .
	ConfigurationUISchema string                 `json:"configurationUISchema" yaml:"configurationUISchema"`
	AdditionalProperties  map[string]interface{} `json:"-" yaml:"-"`
}

// Getter for additional properties for ComponentDefinition_Metadata. Returns the specified
// element and whether it was found
func (a ComponentDefinition_Metadata) Get(fieldName string) (value interface{}, found bool) {
	if a.AdditionalProperties != nil {
		value, found = a.AdditionalProperties[fieldName]
	}
	return
}

// Setter for additional properties for ComponentDefinition_Metadata
func (a *ComponentDefinition_Metadata) Set(fieldName string, value interface{}) {
	if a.AdditionalProperties == nil {
		a.AdditionalProperties = make(map[string]interface{})
	}
	a.AdditionalProperties[fieldName] = value
}

// Override default JSON handling for ComponentDefinition_Metadata to handle AdditionalProperties
func (a *ComponentDefinition_Metadata) UnmarshalJSON(b []byte) error {
	object := make(map[string]json.RawMessage)
	err := json.Unmarshal(b, &object)
	if err != nil {
		return err
	}

	if raw, found := object["genealogy"]; found {
		err = json.Unmarshal(raw, &a.Genealogy)
		if err != nil {
			return fmt.Errorf("error reading 'genealogy': %w", err)
		}
		delete(object, "genealogy")
	}

	if raw, found := object["isAnnotation"]; found {
		err = json.Unmarshal(raw, &a.IsAnnotation)
		if err != nil {
			return fmt.Errorf("error reading 'isAnnotation': %w", err)
		}
		delete(object, "isAnnotation")
	}

	if raw, found := object["isNamespaced"]; found {
		err = json.Unmarshal(raw, &a.IsNamespaced)
		if err != nil {
			return fmt.Errorf("error reading 'isNamespaced': %w", err)
		}
		delete(object, "isNamespaced")
	}

	if raw, found := object["published"]; found {
		err = json.Unmarshal(raw, &a.Published)
		if err != nil {
			return fmt.Errorf("error reading 'published': %w", err)
		}
		delete(object, "published")
	}

	if raw, found := object["instanceDetails"]; found {
		err = json.Unmarshal(raw, &a.InstanceDetails)
		if err != nil {
			return fmt.Errorf("error reading 'instanceDetails': %w", err)
		}
		delete(object, "instanceDetails")
	}

	if raw, found := object["configurationUISchema"]; found {
		err = json.Unmarshal(raw, &a.ConfigurationUISchema)
		if err != nil {
			return fmt.Errorf("error reading 'configurationUISchema': %w", err)
		}
		delete(object, "configurationUISchema")
	}

	if len(object) != 0 {
		a.AdditionalProperties = make(map[string]interface{})
		for fieldName, fieldBuf := range object {
			var fieldVal interface{}
			err := json.Unmarshal(fieldBuf, &fieldVal)
			if err != nil {
				return fmt.Errorf("error unmarshaling field %s: %w", fieldName, err)
			}
			a.AdditionalProperties[fieldName] = fieldVal
		}
	}
	return nil
}

// Override default JSON handling for ComponentDefinition_Metadata to handle AdditionalProperties
func (a ComponentDefinition_Metadata) MarshalJSON() ([]byte, error) {
	var err error
	object := make(map[string]json.RawMessage)

	object["genealogy"], err = json.Marshal(a.Genealogy)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'genealogy': %w", err)
	}

	object["isAnnotation"], err = json.Marshal(a.IsAnnotation)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'isAnnotation': %w", err)
	}

	object["isNamespaced"], err = json.Marshal(a.IsNamespaced)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'isNamespaced': %w", err)
	}

	object["published"], err = json.Marshal(a.Published)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'published': %w", err)
	}

	object["instanceDetails"], err = json.Marshal(a.InstanceDetails)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'instanceDetails': %w", err)
	}

	object["configurationUISchema"], err = json.Marshal(a.ConfigurationUISchema)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'configurationUISchema': %w", err)
	}

	for fieldName, field := range a.AdditionalProperties {
		object[fieldName], err = json.Marshal(field)
		if err != nil {
			return nil, fmt.Errorf("error marshaling '%s': %w", fieldName, err)
		}
	}
	return json.Marshal(object)
}
