// Package registry provides toolkit registration and management.
package registry

import (
	"github.com/modelcontextprotocol/go-sdk/mcp"

	"github.com/txn2/mcp-data-platform/pkg/query"
	"github.com/txn2/mcp-data-platform/pkg/semantic"
)

// Toolkit is the interface that all composable toolkits must implement.
type Toolkit interface {
	// Kind returns the toolkit type (e.g., "trino", "datahub", "s3").
	Kind() string

	// Name returns the instance name from config.
	Name() string

	// Connection returns the connection name for audit logging.
	// This identifies the specific backend connection (e.g., "prod-trino", "main-datahub").
	Connection() string

	// RegisterTools registers all tools with the MCP server.
	RegisterTools(s *mcp.Server)

	// Tools returns a list of tool names provided by this toolkit.
	Tools() []string

	// SetSemanticProvider sets the semantic metadata provider for enrichment.
	SetSemanticProvider(provider semantic.Provider)

	// SetQueryProvider sets the query execution provider for enrichment.
	SetQueryProvider(provider query.Provider)

	// Close releases resources.
	Close() error
}

// ToolkitFactory creates a toolkit from configuration.
type ToolkitFactory func(name string, config map[string]any) (Toolkit, error)

// AggregateToolkitFactory creates a single toolkit from multiple instance configs.
// Used for toolkit kinds that support multi-connection routing internally
// (e.g., Trino with multiserver.Manager).
type AggregateToolkitFactory func(defaultName string, instances map[string]map[string]any) (Toolkit, error)

// PromptInfo describes a registered MCP prompt for metadata collection.
type PromptInfo struct {
	Name        string               `json:"name"`
	Description string               `json:"description"`
	Category    string               `json:"category,omitempty"`
	Content     string               `json:"content,omitempty"`
	Arguments   []PromptArgumentInfo `json:"arguments,omitempty"`
}

// PromptArgumentInfo describes an argument for a registered MCP prompt.
type PromptArgumentInfo struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Required    bool   `json:"required"`
}

// PromptDescriber is an optional interface that toolkits can implement
// to advertise the prompts they register. This allows the platform to
// include toolkit-registered prompts in the platform_info response.
type PromptDescriber interface {
	PromptInfos() []PromptInfo
}

// ConnectionResolver is an optional interface for toolkits that fan
// out across multiple upstream connections (e.g. the mcp gateway).
// When implemented, the registry calls ConnectionForTool to attribute
// each tool to its specific upstream connection in audit rows; an
// empty return falls back to the toolkit's default Connection().
type ConnectionResolver interface {
	ConnectionForTool(toolName string) string
}

// ToolkitConfig holds configuration for a toolkit instance.
type ToolkitConfig struct {
	Kind    string
	Name    string
	Enabled bool
	Config  map[string]any
	Default bool
}
