package types

import (
	composetypes "github.com/compose-spec/compose-go/v2/types"
	v1alpha2 "github.com/kagent-dev/kagent/go/api/v1alpha2"
	kmcpv1alpha1 "github.com/kagent-dev/kmcp/api/v1alpha1"
	corev1 "k8s.io/api/core/v1"
)

type DesiredState struct {
	MCPServers []*MCPServer `json:"mcpServers"`
	Agents     []*Agent     `json:"agents"`
}

type Agent struct {
	Name               string                    `json:"name"`
	Tag                string                    `json:"tag"`
	DeploymentID       string                    `json:"deploymentId,omitempty"`
	Deployment         AgentDeployment           `json:"deployment"`
	ResolvedMCPServers []ResolvedMCPServerConfig `json:"resolvedMCPServers,omitempty"`
	ResolvedPrompts    []ResolvedPrompt          `json:"resolvedPrompts,omitempty"`
	Skills             []AgentSkillRef           `json:"skills,omitempty"`
}

type AgentSkillRef struct {
	Name    string `json:"name,omitempty"`
	Image   string `json:"image,omitempty"`
	RepoURL string `json:"repoURL,omitempty"`
	Ref     string `json:"ref,omitempty"`
	Path    string `json:"path,omitempty"`
}

type ResolvedMCPServerConfig struct {
	Name    string            `json:"name"`
	Type    string            `json:"type"`
	URL     string            `json:"url,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
}

type ResolvedPrompt struct {
	Name    string `json:"name"`
	Content string `json:"content"`
}

type MCPServer struct {
	Name          string           `json:"name"`
	DeploymentID  string           `json:"deploymentId,omitempty"`
	MCPServerType MCPServerType    `json:"mcpServerType"`
	Remote        *RemoteMCPTarget `json:"remote,omitempty"`
	Local         *LocalMCPServer  `json:"local,omitempty"`
	Namespace     string           `json:"namespace,omitempty"`
}

type MCPServerType string

const (
	MCPServerTypeRemote MCPServerType = "remote"
	MCPServerTypeLocal  MCPServerType = "local"
)

// RemoteMCPTarget is the platform-internal DTO describing a remote MCP
// endpoint (parsed URL components + headers). Distinct from the
// kagent.dev/v1alpha2.RemoteMCPServer CRD which is the on-cluster
// representation.
type RemoteMCPTarget struct {
	Scheme  string
	Host    string
	Port    uint32
	Path    string
	Headers []HeaderValue
}

type HeaderValue struct {
	Name  string
	Value string
}

type LocalMCPServer struct {
	Deployment    MCPServerDeployment `json:"deployment"`
	TransportType TransportType       `json:"transportType"`
	HTTP          *HTTPTransport      `json:"http,omitempty"`
}

type HTTPTransport struct {
	Port uint32 `json:"port"`
	Path string `json:"path,omitempty"`
}

type TransportType string

const (
	TransportTypeStdio TransportType = "stdio"
	TransportTypeHTTP  TransportType = "http"
)

type MCPServerDeployment struct {
	Image string            `json:"image,omitempty"`
	Cmd   string            `json:"cmd,omitempty"`
	Args  []string          `json:"args,omitempty"`
	Env   map[string]string `json:"env,omitempty"`
}

type AgentDeployment struct {
	Image string            `json:"image,omitempty"`
	Env   map[string]string `json:"env,omitempty"`
	Port  uint16            `json:"port,omitempty"`
}

type KubernetesRuntimeConfig struct {
	Agents           []*v1alpha2.Agent           `json:"agents"`
	RemoteMCPServers []*v1alpha2.RemoteMCPServer `json:"remoteMCPServers"`
	MCPServers       []*kmcpv1alpha1.MCPServer   `json:"mcpServers"`
	ConfigMaps       []*corev1.ConfigMap         `json:"configMaps,omitempty"`
}

type DockerComposeConfig = composetypes.Project

type LocalRuntimeConfig struct {
	DockerCompose *DockerComposeConfig
	AgentGateway  *AgentGatewayConfig
}
