// Copyright Contributors to the KubeOpenCode project

// Package types provides API types for the KubeOpenCode server.
package types

import (
	"time"

	kubeopenv1alpha1 "github.com/kubeopencode/kubeopencode/api/v1alpha1"
)

// ServerInfo represents server information
type ServerInfo struct {
	Version string `json:"version"`
}

// NamespaceList represents a list of namespaces
type NamespaceList struct {
	Namespaces []string `json:"namespaces"`
}

// AgentReference represents a reference to an Agent
type AgentReference struct {
	Name string `json:"name"`
}

// ContextItem represents a context item in the API
type ContextItem struct {
	Name        string         `json:"name,omitempty"`
	Description string         `json:"description,omitempty"`
	Type        string         `json:"type"`
	Text        string         `json:"text,omitempty"`
	MountPath   string         `json:"mountPath,omitempty"`
	Sync        *GitSyncConfig `json:"sync,omitempty"`
}

// GitSyncConfig represents the sync configuration of a Git context
type GitSyncConfig struct {
	Enabled  bool   `json:"enabled"`
	Interval string `json:"interval,omitempty"`
	Policy   string `json:"policy,omitempty"`
}

// AgentTemplateReference represents a reference to an AgentTemplate
type AgentTemplateReference struct {
	Name string `json:"name"`
}

// CreateTaskRequest represents a request to create a task
type CreateTaskRequest struct {
	Name        string                  `json:"name,omitempty"`
	Description string                  `json:"description,omitempty"`
	AgentRef    *AgentReference         `json:"agentRef,omitempty"`
	TemplateRef *AgentTemplateReference `json:"templateRef,omitempty"`
	Timeout     string                  `json:"timeout,omitempty"`
	Contexts    []ContextItem           `json:"contexts,omitempty"`
}

// CreateAgentRequest represents a request to create an agent
type CreateAgentRequest struct {
	Name               string          `json:"name"`
	Profile            string          `json:"profile,omitempty"`
	TemplateRef        *AgentReference `json:"templateRef,omitempty"`
	WorkspaceDir       string          `json:"workspaceDir,omitempty"`
	ServiceAccountName string          `json:"serviceAccountName,omitempty"`

	// P0: Images (required when no template)
	AgentImage    string `json:"agentImage,omitempty"`
	ExecutorImage string `json:"executorImage,omitempty"`

	// P1: Common configuration
	MaxConcurrentTasks *int32                   `json:"maxConcurrentTasks,omitempty"`
	Standby            *StandbyInfo             `json:"standby,omitempty"`
	Persistence        *CreatePersistenceConfig `json:"persistence,omitempty"`

	// P2: Advanced configuration
	Port  *int32           `json:"port,omitempty"`
	Proxy *ProxyConfigInfo `json:"proxy,omitempty"`

	// Assembly: assets selected from a Registry catalog
	Skills  []kubeopenv1alpha1.SkillSource `json:"skills,omitempty"`
	Plugins []kubeopenv1alpha1.PluginSpec  `json:"plugins,omitempty"`
}

// CreatePersistenceConfig represents persistence settings in create request
type CreatePersistenceConfig struct {
	Sessions  *CreateVolumePersistence `json:"sessions,omitempty"`
	Workspace *CreateVolumePersistence `json:"workspace,omitempty"`
}

// CreateVolumePersistence represents volume persistence settings in create request
type CreateVolumePersistence struct {
	StorageClassName string `json:"storageClassName,omitempty"`
	Size             string `json:"size,omitempty"`
}

// TaskResponse represents a task in API responses
type TaskResponse struct {
	Name           string                  `json:"name"`
	Namespace      string                  `json:"namespace"`
	Phase          string                  `json:"phase"`
	Description    string                  `json:"description,omitempty"`
	AgentRef       *AgentReference         `json:"agentRef,omitempty"`
	TemplateRef    *AgentTemplateReference `json:"templateRef,omitempty"`
	Timeout        string                  `json:"timeout,omitempty"`
	PodName        string                  `json:"podName,omitempty"`
	Session        *SessionInfoResponse    `json:"session,omitempty"`
	StartTime      *time.Time              `json:"startTime,omitempty"`
	CompletionTime *time.Time              `json:"completionTime,omitempty"`
	Duration       string                  `json:"duration,omitempty"`
	CreatedAt      time.Time               `json:"createdAt"`
	Conditions     []Condition             `json:"conditions,omitempty"`
	Labels         map[string]string       `json:"labels,omitempty"`
}

// SessionInfoResponse represents session information in API responses
type SessionInfoResponse struct {
	ID      string                  `json:"id,omitempty"`
	Title   string                  `json:"title,omitempty"`
	Summary *SessionSummaryResponse `json:"summary,omitempty"`
}

// SessionSummaryResponse represents session summary statistics
type SessionSummaryResponse struct {
	MessageCount int32               `json:"messageCount,omitempty"`
	TokenUsage   *TokenUsageResponse `json:"tokenUsage,omitempty"`
	Cost         string              `json:"cost,omitempty"`
	FilesChanged int32               `json:"filesChanged,omitempty"`
	Additions    int32               `json:"additions,omitempty"`
	Deletions    int32               `json:"deletions,omitempty"`
}

// TokenUsageResponse represents token usage in API responses
type TokenUsageResponse struct {
	Input     int64 `json:"input,omitempty"`
	Output    int64 `json:"output,omitempty"`
	Reasoning int64 `json:"reasoning,omitempty"`
	Cache     int64 `json:"cache,omitempty"`
}

// Pagination represents pagination metadata
type Pagination struct {
	Limit      int  `json:"limit"`
	Offset     int  `json:"offset"`
	TotalCount int  `json:"totalCount"`
	HasMore    bool `json:"hasMore"`
}

// TaskListResponse represents a list of tasks
type TaskListResponse struct {
	Tasks      []TaskResponse `json:"tasks"`
	Total      int            `json:"total"` // Keep for backward compat
	Pagination *Pagination    `json:"pagination,omitempty"`
}

// Condition represents a status condition
type Condition struct {
	Type    string `json:"type"`
	Status  string `json:"status"`
	Reason  string `json:"reason,omitempty"`
	Message string `json:"message,omitempty"`
}

// CredentialInfo represents credential information (without secrets)
type CredentialInfo struct {
	Name      string `json:"name"`
	SecretRef string `json:"secretRef"`
	MountPath string `json:"mountPath,omitempty"`
	Env       string `json:"env,omitempty"`
}

// QuotaInfo represents quota configuration
type QuotaInfo struct {
	MaxTaskStarts int32 `json:"maxTaskStarts,omitempty"`
	WindowSeconds int32 `json:"windowSeconds,omitempty"`
}

// SkillInfo represents a skill source in API responses
type SkillInfo struct {
	Name string        `json:"name"`
	Git  *GitSkillInfo `json:"git,omitempty"`
}

// GitSkillInfo represents Git source details for a skill
type GitSkillInfo struct {
	Repository string   `json:"repository"`
	Ref        string   `json:"ref,omitempty"`
	Path       string   `json:"path,omitempty"`
	Names      []string `json:"names,omitempty"`
}

// PluginInfo represents a plugin in API responses
type PluginInfo struct {
	Name    string                 `json:"name"`
	Target  string                 `json:"target,omitempty"`
	Options map[string]interface{} `json:"options,omitempty"`
}

// AgentResponse represents an agent in API responses
type AgentResponse struct {
	Name               string                 `json:"name"`
	Namespace          string                 `json:"namespace"`
	Profile            string                 `json:"profile,omitempty"`
	TemplateRef        *AgentReference        `json:"templateRef,omitempty"`
	ExecutorImage      string                 `json:"executorImage,omitempty"`
	AgentImage         string                 `json:"agentImage,omitempty"`
	WorkspaceDir       string                 `json:"workspaceDir,omitempty"`
	ServiceAccountName string                 `json:"serviceAccountName,omitempty"`
	ContextsCount      int                    `json:"contextsCount"`
	CredentialsCount   int                    `json:"credentialsCount"`
	SkillsCount        int                    `json:"skillsCount"`
	PluginsCount       int                    `json:"pluginsCount"`
	MaxConcurrentTasks *int32                 `json:"maxConcurrentTasks,omitempty"`
	Quota              *QuotaInfo             `json:"quota,omitempty"`
	Credentials        []CredentialInfo       `json:"credentials,omitempty"`
	Contexts           []ContextItem          `json:"contexts,omitempty"`
	Skills             []SkillInfo            `json:"skills,omitempty"`
	Plugins            []PluginInfo           `json:"plugins,omitempty"`
	Config             map[string]interface{} `json:"config,omitempty"`
	CreatedAt          time.Time              `json:"createdAt"`
	Labels             map[string]string      `json:"labels,omitempty"`
	Standby            *StandbyInfo           `json:"standby,omitempty"`
	Share              *ShareStatusInfo       `json:"share,omitempty"`
	Conditions         []Condition            `json:"conditions,omitempty"`
	ServerStatus       *ServerStatusInfo      `json:"serverStatus,omitempty"`
}

// AgentTemplateResponse represents an agent template in API responses
type AgentTemplateResponse struct {
	Name               string                 `json:"name"`
	Namespace          string                 `json:"namespace"`
	AgentImage         string                 `json:"agentImage,omitempty"`
	ExecutorImage      string                 `json:"executorImage,omitempty"`
	WorkspaceDir       string                 `json:"workspaceDir,omitempty"`
	ServiceAccountName string                 `json:"serviceAccountName,omitempty"`
	ContextsCount      int                    `json:"contextsCount"`
	CredentialsCount   int                    `json:"credentialsCount"`
	SkillsCount        int                    `json:"skillsCount"`
	PluginsCount       int                    `json:"pluginsCount"`
	Credentials        []CredentialInfo       `json:"credentials,omitempty"`
	Contexts           []ContextItem          `json:"contexts,omitempty"`
	Skills             []SkillInfo            `json:"skills,omitempty"`
	Plugins            []PluginInfo           `json:"plugins,omitempty"`
	Config             map[string]interface{} `json:"config,omitempty"`
	CreatedAt          time.Time              `json:"createdAt"`
	Labels             map[string]string      `json:"labels,omitempty"`
	Conditions         []Condition            `json:"conditions,omitempty"`
	AgentCount         int                    `json:"agentCount"`
}

// AgentTemplateListResponse represents a list of agent templates
type AgentTemplateListResponse struct {
	Templates  []AgentTemplateResponse `json:"templates"`
	Total      int                     `json:"total"`
	Pagination *Pagination             `json:"pagination,omitempty"`
}

// StandbyInfo represents standby configuration in API requests/responses
type StandbyInfo struct {
	IdleTimeout string `json:"idleTimeout"`
}

// ServerStatusInfo represents the status of an Agent's deployment
type ServerStatusInfo struct {
	DeploymentName  string              `json:"deploymentName,omitempty"`
	ServiceName     string              `json:"serviceName,omitempty"`
	URL             string              `json:"url,omitempty"`
	Ready           bool                `json:"ready"`
	Port            int32               `json:"port,omitempty"`
	Suspended       bool                `json:"suspended"`
	IdleSince       *time.Time          `json:"idleSince,omitempty"`
	GitSyncStatuses []GitSyncStatusInfo `json:"gitSyncStatuses,omitempty"`
}

// GitSyncStatusInfo represents the sync state of a Git context
type GitSyncStatusInfo struct {
	Name       string     `json:"name"`
	CommitHash string     `json:"commitHash,omitempty"`
	LastSynced *time.Time `json:"lastSynced,omitempty"`
}

// LogEvent represents a Server-Sent Event for log streaming
type LogEvent struct {
	Type     string  `json:"type"`
	Phase    *string `json:"phase,omitempty"`
	PodPhase *string `json:"podPhase,omitempty"`
	Content  *string `json:"content,omitempty"`
	Message  string  `json:"message,omitempty"`
}

// HealthResponse represents the health endpoint response
type HealthResponse struct {
	Status  string `json:"status"`
	Version string `json:"version"`
	Uptime  string `json:"uptime"`
}

// AgentListResponse represents a list of agents
type AgentListResponse struct {
	Agents     []AgentResponse `json:"agents"`
	Total      int             `json:"total"`
	Pagination *Pagination     `json:"pagination,omitempty"`
}

// ConfigResponse represents the KubeOpenCodeConfig in API responses
type ConfigResponse struct {
	Name        string             `json:"name"`
	CreatedAt   time.Time          `json:"createdAt"`
	SystemImage *SystemImageConfig `json:"systemImage,omitempty"`
	Cleanup     *CleanupConfig     `json:"cleanup,omitempty"`
	Proxy       *ProxyConfigInfo   `json:"proxy,omitempty"`
	Labels      map[string]string  `json:"labels,omitempty"`
}

// SystemImageConfig represents system image configuration
type SystemImageConfig struct {
	Image           string `json:"image,omitempty"`
	ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
}

// CleanupConfig represents cleanup configuration
type CleanupConfig struct {
	TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty"`
	MaxRetainedTasks        *int32 `json:"maxRetainedTasks,omitempty"`
}

// ProxyConfigInfo represents proxy configuration in API responses
type ProxyConfigInfo struct {
	HttpProxy  string `json:"httpProxy,omitempty"`  //nolint:staticcheck,revive // JSON API field name convention
	HttpsProxy string `json:"httpsProxy,omitempty"` //nolint:staticcheck,revive // JSON API field name convention
	NoProxy    string `json:"noProxy,omitempty"`
}

// CreateCronTaskRequest represents a request to create a CronTask
type CreateCronTaskRequest struct {
	Name                    string                  `json:"name,omitempty"`
	Schedule                string                  `json:"schedule"`
	TimeZone                string                  `json:"timeZone,omitempty"`
	ConcurrencyPolicy       string                  `json:"concurrencyPolicy,omitempty"`
	Suspend                 bool                    `json:"suspend,omitempty"`
	StartingDeadlineSeconds *int64                  `json:"startingDeadlineSeconds,omitempty"`
	MaxRetainedTasks        *int32                  `json:"maxRetainedTasks,omitempty"`
	Description             string                  `json:"description,omitempty"`
	AgentRef                *AgentReference         `json:"agentRef,omitempty"`
	TemplateRef             *AgentTemplateReference `json:"templateRef,omitempty"`
	Contexts                []ContextItem           `json:"contexts,omitempty"`
}

// UpdateCronTaskRequest represents a request to update a CronTask
type UpdateCronTaskRequest struct {
	Schedule                string  `json:"schedule,omitempty"`
	TimeZone                *string `json:"timeZone,omitempty"`
	ConcurrencyPolicy       string  `json:"concurrencyPolicy,omitempty"`
	Suspend                 *bool   `json:"suspend,omitempty"`
	StartingDeadlineSeconds *int64  `json:"startingDeadlineSeconds,omitempty"`
	MaxRetainedTasks        *int32  `json:"maxRetainedTasks,omitempty"`
	Description             *string `json:"description,omitempty"`
}

// CronTaskResponse represents a CronTask in API responses
type CronTaskResponse struct {
	Name                    string               `json:"name"`
	Namespace               string               `json:"namespace"`
	Schedule                string               `json:"schedule"`
	TimeZone                string               `json:"timeZone,omitempty"`
	ConcurrencyPolicy       string               `json:"concurrencyPolicy"`
	Suspend                 bool                 `json:"suspend"`
	MaxRetainedTasks        int32                `json:"maxRetainedTasks,omitempty"`
	StartingDeadlineSeconds *int64               `json:"startingDeadlineSeconds,omitempty"`
	Active                  int32                `json:"active"`
	LastScheduleTime        *time.Time           `json:"lastScheduleTime,omitempty"`
	LastSuccessfulTime      *time.Time           `json:"lastSuccessfulTime,omitempty"`
	NextScheduleTime        *time.Time           `json:"nextScheduleTime,omitempty"`
	TotalExecutions         int64                `json:"totalExecutions"`
	TaskTemplate            CronTaskTemplateInfo `json:"taskTemplate"`
	CreatedAt               time.Time            `json:"createdAt"`
	Labels                  map[string]string    `json:"labels,omitempty"`
	Conditions              []Condition          `json:"conditions,omitempty"`
}

// CronTaskTemplateInfo represents the task template in CronTask responses
type CronTaskTemplateInfo struct {
	Description string                  `json:"description,omitempty"`
	AgentRef    *AgentReference         `json:"agentRef,omitempty"`
	TemplateRef *AgentTemplateReference `json:"templateRef,omitempty"`
}

// CronTaskListResponse represents a list of CronTasks
type CronTaskListResponse struct {
	CronTasks  []CronTaskResponse `json:"cronTasks"`
	Total      int                `json:"total"`
	Pagination *Pagination        `json:"pagination,omitempty"`
}

// CreateAgentTemplateRequest represents a request to create an agent template
type CreateAgentTemplateRequest struct {
	Name               string `json:"name"`
	WorkspaceDir       string `json:"workspaceDir,omitempty"`
	ServiceAccountName string `json:"serviceAccountName,omitempty"`
	AgentImage         string `json:"agentImage,omitempty"`
	ExecutorImage      string `json:"executorImage,omitempty"`

	// Assembly: assets selected from a Registry catalog
	Skills  []kubeopenv1alpha1.SkillSource `json:"skills,omitempty"`
	Plugins []kubeopenv1alpha1.PluginSpec  `json:"plugins,omitempty"`
}

// ShareTokenResponse is returned by GET /agents/{name}/share with the actual token value
type ShareTokenResponse struct {
	Enabled    bool       `json:"enabled"`
	Active     bool       `json:"active"`
	Token      string     `json:"token,omitempty"`
	Path       string     `json:"path,omitempty"`
	ExpiresAt  *time.Time `json:"expiresAt,omitempty"`
	AllowedIPs []string   `json:"allowedIPs,omitempty"`
}

// UpdateShareRequest is the request body for POST /agents/{name}/share
type UpdateShareRequest struct {
	Enabled    bool     `json:"enabled"`
	ExpiresIn  string   `json:"expiresIn,omitempty"` // Go duration string (e.g., "24h")
	AllowedIPs []string `json:"allowedIPs,omitempty"`
}

// ShareInfoResponse represents agent info returned for share link pages
type ShareInfoResponse struct {
	AgentName string  `json:"agentName"`
	Namespace string  `json:"namespace"`
	Profile   string  `json:"profile,omitempty"`
	ExpiresAt *string `json:"expiresAt,omitempty"`
}

// ShareStatusInfo represents share configuration in API responses
type ShareStatusInfo struct {
	Enabled    bool       `json:"enabled"`
	Active     bool       `json:"active"`
	SecretName string     `json:"secretName,omitempty"`
	URL        string     `json:"url,omitempty"`
	ExpiresAt  *time.Time `json:"expiresAt,omitempty"`
	AllowedIPs []string   `json:"allowedIPs,omitempty"`
}

// RegistryResponse represents a Registry in API responses
type RegistryResponse struct {
	Name       string               `json:"name"`
	Namespace  string               `json:"namespace"`
	Images     []RegistryImageInfo  `json:"images,omitempty"`
	Skills     []RegistrySkillInfo  `json:"skills,omitempty"`
	Plugins    []RegistryPluginInfo `json:"plugins,omitempty"`
	Summary    RegistrySummaryInfo  `json:"summary"`
	CreatedAt  time.Time            `json:"createdAt"`
	Labels     map[string]string    `json:"labels,omitempty"`
	Conditions []Condition          `json:"conditions,omitempty"`
}

// RegistryImageInfo represents an image entry in a Registry response
type RegistryImageInfo struct {
	Name        string             `json:"name"`
	Image       string             `json:"image"`
	Phase       string             `json:"phase"`
	Digest      string             `json:"digest,omitempty"`
	LastChecked *time.Time         `json:"lastChecked,omitempty"`
	Message     string             `json:"message,omitempty"`
	Metadata    *ImageMetadataInfo `json:"metadata,omitempty"`
}

// ImageMetadataInfo represents image metadata in API responses
type ImageMetadataInfo struct {
	Description string   `json:"description,omitempty"`
	Category    string   `json:"category,omitempty"`
	Tags        []string `json:"tags,omitempty"`
	Tools       []string `json:"tools,omitempty"`
	BaseImage   string   `json:"baseImage,omitempty"`
	Maintainer  string   `json:"maintainer,omitempty"`
}

// RegistrySkillInfo represents a skill entry in a Registry response
type RegistrySkillInfo struct {
	Name         string     `json:"name"`
	Repository   string     `json:"repository,omitempty"`
	Ref          string     `json:"ref,omitempty"`
	Path         string     `json:"path,omitempty"`
	Names        []string   `json:"names,omitempty"`
	Phase        string     `json:"phase"`
	LatestCommit string     `json:"latestCommit,omitempty"`
	LastChecked  *time.Time `json:"lastChecked,omitempty"`
	Message      string     `json:"message,omitempty"`
	Description  string     `json:"description,omitempty"`
	Tags         []string   `json:"tags,omitempty"`
}

// RegistryPluginInfo represents a plugin entry in a Registry response
type RegistryPluginInfo struct {
	Name            string     `json:"name"`
	Package         string     `json:"package"`
	Target          string     `json:"target,omitempty"`
	Phase           string     `json:"phase"`
	ResolvedVersion string     `json:"resolvedVersion,omitempty"`
	LastChecked     *time.Time `json:"lastChecked,omitempty"`
	Message         string     `json:"message,omitempty"`
	Description     string     `json:"description,omitempty"`
	Tags            []string   `json:"tags,omitempty"`
}

// RegistrySummaryInfo represents aggregate asset counts
type RegistrySummaryInfo struct {
	Images     int `json:"images"`
	Skills     int `json:"skills"`
	Plugins    int `json:"plugins"`
	ReadyCount int `json:"readyCount"`
	TotalCount int `json:"totalCount"`
}

// RegistryListResponse represents a list of Registries
type RegistryListResponse struct {
	Registries []RegistryResponse `json:"registries"`
	Total      int                `json:"total"`
	Pagination *Pagination        `json:"pagination,omitempty"`
}

// CreateRegistryRequest represents a request to create a Registry
type CreateRegistryRequest struct {
	Name string `json:"name"`
}

// ErrorResponse represents an error response
type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
	Code    int    `json:"code"`
}
