/**
 * Serialized Registry Types
 *
 * Pre-computed JSON-safe types for API responses and Command View.
 * Serialization happens once at API startup, enabling instant response times.
 */

import { CommandViewData } from './command-view'
import type {
  ResourceDefinition,
  TriggerDefinition,
  IntegrationDefinition,
  ResourceRelationships,
  ExternalResourceDefinition,
  HumanCheckpointDefinition
} from './types'
import type { ResourceCategory, ResourceLink } from './resource-link'

// ============================================================================
// Serialized Definition Types
// ============================================================================

/**
 * Serialized agent definition (JSON-safe)
 * Result of serializeDefinition(AgentDefinition)
 */
export interface SerializedAgentDefinition {
  config: {
    resourceId: string
    name: string
    description: string
    version: string
    type: 'agent'
    kind: 'orchestrator' | 'specialist' | 'utility' | 'system'
    status: 'dev' | 'prod'
    links?: ResourceLink[]
    category?: ResourceCategory
    /** Whether this resource is archived and should be excluded from registration and deployment */
    archived?: boolean
    systemPrompt: string
    constraints?: {
      maxIterations?: number
      timeout?: number
      maxSessionMemoryKeys?: number
      maxMemoryTokens?: number
    }
    sessionCapable?: boolean
    memoryPreferences?: string
  }
  modelConfig: {
    provider: string
    model: string
    apiKey: string // Redacted: "sk-proj..."
    temperature: number
    maxOutputTokens: number
    topP?: number
    modelOptions?: Record<string, unknown>
  }
  contract: {
    inputSchema: object // JSON Schema
    outputSchema?: object // JSON Schema
  }
  tools: Array<{
    name: string
    description: string
    inputSchema?: object // JSON Schema
    outputSchema?: object // JSON Schema
  }>
  knowledgeMap?: {
    nodeCount: number
    nodes: Array<{
      id: string
      description: string
      loaded: boolean
      hasPrompt: boolean
    }>
  }
  metricsConfig?: object
}

/**
 * Serialized workflow definition (JSON-safe)
 * Result of serializeDefinition(WorkflowDefinition)
 */
export interface SerializedWorkflowDefinition {
  config: {
    resourceId: string
    name: string
    description: string
    version: string
    type: 'workflow'
    status: 'dev' | 'prod'
    links?: ResourceLink[]
    category?: ResourceCategory
    /** Whether this resource is archived and should be excluded from registration and deployment */
    archived?: boolean
  }
  entryPoint: string
  steps: Array<{
    id: string
    name: string
    description: string
    inputSchema?: object // JSON Schema
    outputSchema?: object // JSON Schema
    next: {
      type: 'linear' | 'conditional'
      target?: string
      routes?: Array<{ target: string }>
      default?: string
    } | null
  }>
  contract: {
    inputSchema: object // JSON Schema
    outputSchema?: object // JSON Schema
  }
  metricsConfig?: object
}

// ============================================================================
// Command View Types (re-exported from command-view.ts)
// ============================================================================

export type {
  CommandViewAgent,
  CommandViewWorkflow,
  CommandViewNode,
  CommandViewEdge,
  CommandViewData,
  RelationshipType
} from './command-view'

// ============================================================================
// Serialized Organization Data
// ============================================================================

/**
 * Complete pre-serialized organization data
 * Computed once at startup, served instantly
 */
export interface SerializedOrganizationData {
  /** Deployment version (semver) */
  version: string

  /** Basic resource lists (for /resources endpoint) */
  resources: {
    workflows: ResourceDefinition[]
    agents: ResourceDefinition[]
    total: number
  }

  /** Full serialized definitions (for /resources/:id/definition endpoint) */
  definitions: {
    workflows: Map<string, SerializedWorkflowDefinition>
    agents: Map<string, SerializedAgentDefinition>
  }

  /** Command View data (for /command-view endpoint) */
  commandView: CommandViewData

  /** Pass-through manifest data (already JSON-safe) */
  triggers: TriggerDefinition[]
  integrations: IntegrationDefinition[]
  externalResources: ExternalResourceDefinition[]
  humanCheckpoints: HumanCheckpointDefinition[]
  relationships: ResourceRelationships | undefined
}
