package config

// AdaptersConf holds both legacy single-instance adapter fields and the
// multi-instance collections. Legacy fields are normalised into the slice
// fields during config load so that all downstream code only needs to
// inspect the slice fields.
type AdaptersConf struct {
	// Legacy single-instance fields. When non-empty they are normalised into
	// OllamaInstances / VllmInstances / OpenAICompatInstances during config load
	// so that all downstream code only needs to inspect the slice fields.
	Ollama       OllamaConf       `mapstructure:"ollama"        yaml:"ollama"`
	Vllm         VllmConf         `mapstructure:"vllm"          yaml:"vllm"`
	OpenAICompat OpenAICompatConf `mapstructure:"openai_compat" yaml:"openai_compat"`
	Mock         MockConf         `mapstructure:"mock"          yaml:"mock"`

	// Multi-instance collections. Each entry carries a unique Name that acts as
	// the stable adapter instance identity within the node. Names must be unique
	// within each type collection; duplicate names are rejected at load time.
	OllamaInstances       []OllamaInstanceConf       `mapstructure:"ollama_instances"        yaml:"ollama_instances,omitempty"`
	VllmInstances         []VllmInstanceConf         `mapstructure:"vllm_instances"          yaml:"vllm_instances,omitempty"`
	OpenAICompatInstances []OpenAICompatInstanceConf `mapstructure:"openai_compat_instances" yaml:"openai_compat_instances,omitempty"`
}

// OllamaInstanceConf is one named Ollama engine instance within a node.
type OllamaInstanceConf struct {
	Name             string `mapstructure:"name"               yaml:"name"`
	Enabled          bool   `mapstructure:"enabled"            yaml:"enabled"`
	BaseURL          string `mapstructure:"base_url"           yaml:"base_url"`
	ContextSize      int    `mapstructure:"context_size"       yaml:"context_size"`
	Capacity         int    `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int    `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int    `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int    `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
}

// VllmInstanceConf is one named vLLM engine instance within a node.
type VllmInstanceConf struct {
	Name             string `mapstructure:"name"               yaml:"name"`
	Enabled          bool   `mapstructure:"enabled"            yaml:"enabled"`
	Endpoint         string `mapstructure:"endpoint"           yaml:"endpoint"`
	Capacity         int    `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int    `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int    `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int    `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
}

// OpenAICompatConf configures the legacy single-instance OpenAI-compatible adapter.
type OpenAICompatConf struct {
	Enabled          bool              `mapstructure:"enabled"            yaml:"enabled"`
	Provider         string            `mapstructure:"provider"           yaml:"provider"`
	Endpoint         string            `mapstructure:"endpoint"           yaml:"endpoint"`
	Headers          map[string]string `mapstructure:"headers"            yaml:"headers"`
	Capacity         int               `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int               `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int               `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int               `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
	// RuntimeProfile is the resolved concrete protocol profile snapshot carried
	// from the Edge config payload. When non-nil, the adapter resolves operation
	// URLs from the profile's operation paths instead of the legacy endpoint +
	// /v1 suffix heuristic. It is excluded from YAML serialization.
	RuntimeProfile *ConcreteProtocolProfile `mapstructure:"-" yaml:"-"`
}

// OpenAICompatInstanceConf is one named OpenAI-compatible adapter instance within a node.
type OpenAICompatInstanceConf struct {
	Name             string            `mapstructure:"name"               yaml:"name"`
	Enabled          bool              `mapstructure:"enabled"            yaml:"enabled"`
	Provider         string            `mapstructure:"provider"           yaml:"provider"`
	Endpoint         string            `mapstructure:"endpoint"           yaml:"endpoint"`
	Headers          map[string]string `mapstructure:"headers"            yaml:"headers"`
	Capacity         int               `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int               `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int               `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int               `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
}

// OllamaConf configures the legacy single-instance Ollama adapter.
type OllamaConf struct {
	Enabled          bool   `mapstructure:"enabled"            yaml:"enabled"`
	BaseURL          string `mapstructure:"base_url"           yaml:"base_url"`
	ContextSize      int    `mapstructure:"context_size"       yaml:"context_size"`
	Capacity         int    `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int    `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int    `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int    `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
}

// VllmConf configures the legacy single-instance vLLM adapter.
type VllmConf struct {
	Enabled          bool   `mapstructure:"enabled"            yaml:"enabled"`
	Endpoint         string `mapstructure:"endpoint"           yaml:"endpoint"`
	Capacity         int    `mapstructure:"capacity"           yaml:"capacity"`
	MaxQueue         int    `mapstructure:"max_queue"          yaml:"max_queue"`
	QueueTimeoutMS   int    `mapstructure:"queue_timeout_ms"   yaml:"queue_timeout_ms"` // zero means no queue timeout
	RequestTimeoutMS int    `mapstructure:"request_timeout_ms" yaml:"request_timeout_ms"`
}

// MockConf defines the mock adapter instance. It has no execution fields and
// only serves as a flag to enable the mock adapter in the payload.
type MockConf struct {
	Enabled bool `mapstructure:"enabled"  yaml:"enabled"`
}
