import {
  AGENT_TYPES,
  IAgentConfig,
  AgentCategory,
  ConnectionProfile,
  ReasoningProfile,
  SafetyTier,
  EvolutionMode,
} from './types/index';
import {
  TOOLS,
  UNIVERSAL_SYSTEM_TOOLS,
  DEVELOPER_TOOLS,
  AWS_TOOLS,
  WEB_TOOLS,
} from './constants/tools';
import { TRUST } from './constants/system';
import {
  SUPERCLAW_SYSTEM_PROMPT,
  CODER_SYSTEM_PROMPT,
  PLANNER_SYSTEM_PROMPT,
  REFLECTOR_SYSTEM_PROMPT,
  QA_SYSTEM_PROMPT,
  CRITIC_SYSTEM_PROMPT,
  FACILITATOR_SYSTEM_PROMPT,
  MERGER_SYSTEM_PROMPT,
  RESEARCHER_SYSTEM_PROMPT,
  JUDGE_SYSTEM_PROMPT,
} from '../agents/prompts/index';

/**
 * Backbone Registry: The single source of truth for essential system components.
 * Reserved for LLM-based Agents and logic-based Handlers.
 * All tools assigned to agents follow camelCase naming conventions.
 */

const BACKBONE_LLM_CONFIG = {};

const BACKBONE_LLM_DEFAULTS = {
  isBackbone: true,
  parallelToolCalls: false,
  maxIterations: 10,
  safetyTier: SafetyTier.PROD,
  connectionProfile: [],
  trustScore: TRUST.DEFAULT_SCORE,
  evolutionMode: EvolutionMode.HITL,
};

const BACKBONE_HANDLER_DEFAULTS = {
  isBackbone: true,
  parallelToolCalls: false,
  evolutionMode: EvolutionMode.HITL,
  maxIterations: 10,
  safetyTier: SafetyTier.PROD,
  connectionProfile: [],
  trustScore: TRUST.DEFAULT_SCORE,
};

export const BACKBONE_REGISTRY: Record<string, IAgentConfig> = {
  [AGENT_TYPES.SUPERCLAW]: {
    id: AGENT_TYPES.SUPERCLAW,
    name: 'System Orchestrator',
    systemPrompt: SUPERCLAW_SYSTEM_PROMPT,
    description: 'Orchestrator node. Directs traffic, retrieves memory, and delegates tasks.',
    category: AgentCategory.SYSTEM,
    icon: 'Bot',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.STANDARD,
    defaultCommunicationMode: 'text',
    evolutionMode: EvolutionMode.AUTO,
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      TOOLS.dispatchTask,
      TOOLS.signalOrchestration,
      TOOLS.listAgents,
      TOOLS.renderComponent,
      TOOLS.navigateTo,
      TOOLS.uiAction,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'GATEWAY',
      icon: 'Bot',
    },
  },
  [AGENT_TYPES.CODER]: {
    id: AGENT_TYPES.CODER,
    name: 'Coder Agent',
    systemPrompt: CODER_SYSTEM_PROMPT,
    description: 'Autonomous builder. Implements changes and validates via pre-flight checks.',
    category: AgentCategory.SYSTEM,
    icon: 'Code',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.DEEP,
    defaultCommunicationMode: 'json',
    safetyTier: SafetyTier.LOCAL,
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      ...DEVELOPER_TOOLS,
      ...AWS_TOOLS,
      ...WEB_TOOLS,
      TOOLS.runTests,
      TOOLS.runShellCommand,
      TOOLS.stageChanges,
      TOOLS.generatePatch,
      TOOLS.triggerDeployment,
      TOOLS.validateCode,
      TOOLS.checkHealth,
      TOOLS.inspectTrace,
      TOOLS.inspectTopology,
      TOOLS.requestResearch,
      TOOLS.renderComponent,
      TOOLS.uiAction,
    ],
    maxIterations: 50,
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.STORAGE,
      ConnectionProfile.CODEBUILD,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Code',
    },
  },
  [AGENT_TYPES.STRATEGIC_PLANNER]: {
    id: AGENT_TYPES.STRATEGIC_PLANNER,
    name: 'Strategic Planner',
    systemPrompt: PLANNER_SYSTEM_PROMPT,
    description: 'Design node. Identifies missing features and architecting upgrades.',
    category: AgentCategory.SYSTEM,
    icon: 'Brain',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.DEEP,
    defaultCommunicationMode: 'json',
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      TOOLS.signalOrchestration,
      TOOLS.technicalResearch,
      TOOLS.dispatchTask,
      TOOLS.manageGap,
      TOOLS.reportGap,
      TOOLS.requestResearch,
      TOOLS.listAgents,
      TOOLS.createAgent,
      TOOLS.deleteAgent,
      TOOLS.syncAgentRegistry,
      TOOLS.manageAgentTools,
      TOOLS.inspectTopology,
      TOOLS.checkConfig,
      TOOLS.registerMCPServer,
      TOOLS.triggerInfraRebuild,
      TOOLS.triggerBatchEvolution,
      TOOLS.createCollaboration,
      TOOLS.joinCollaboration,
      TOOLS.getCollaborationContext,
      TOOLS.writeToCollaboration,
      TOOLS.closeCollaboration,
      TOOLS.listMyCollaborations,
      TOOLS.renderComponent,
      TOOLS.uiAction,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Brain',
    },
  },
  [AGENT_TYPES.COGNITION_REFLECTOR]: {
    id: AGENT_TYPES.COGNITION_REFLECTOR,
    name: 'Cognition Reflector',
    systemPrompt: REFLECTOR_SYSTEM_PROMPT,
    description: 'Intelligence audit node. Extracts facts, lessons, and gaps from logs.',
    category: AgentCategory.SYSTEM,
    icon: 'Search',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.STANDARD,
    defaultCommunicationMode: 'json',
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      TOOLS.manageGap,
      TOOLS.reportGap,
      TOOLS.prioritizeMemory,
      TOOLS.deleteTraces,
      TOOLS.inspectTrace,
      TOOLS.scanMetabolism,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Search',
    },
  },
  [AGENT_TYPES.QA]: {
    id: AGENT_TYPES.QA,
    name: 'QA Auditor',
    systemPrompt: QA_SYSTEM_PROMPT,
    description: 'Validation node. Audits deployments to ensure they actually solve the gaps.',
    category: AgentCategory.SYSTEM,
    icon: 'FlaskConical',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.STANDARD,
    defaultCommunicationMode: 'json',
    tools: [...UNIVERSAL_SYSTEM_TOOLS, TOOLS.checkHealth, TOOLS.triggerTrunkSync],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'FlaskConical',
    },
  },
  [AGENT_TYPES.CRITIC]: {
    id: AGENT_TYPES.CRITIC,
    name: 'Critic Agent',
    systemPrompt: CRITIC_SYSTEM_PROMPT,
    description:
      'Peer review node. Performs security, performance, and architectural reviews for the Council of Agents.',
    category: AgentCategory.SYSTEM,
    icon: 'Shield',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.THINKING,
    defaultCommunicationMode: 'json',
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      ...DEVELOPER_TOOLS,
      TOOLS.createCollaboration,
      TOOLS.joinCollaboration,
      TOOLS.getCollaborationContext,
      TOOLS.writeToCollaboration,
      TOOLS.closeCollaboration,
      TOOLS.listMyCollaborations,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Shield',
    },
  },
  [AGENT_TYPES.FACILITATOR]: {
    id: AGENT_TYPES.FACILITATOR,
    name: 'Facilitator',
    systemPrompt: FACILITATOR_SYSTEM_PROMPT,
    description: 'Moderator node. Manages multi-party collaboration sessions and drives consensus.',
    category: AgentCategory.SYSTEM,
    icon: 'MessageSquareShare',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.STANDARD,
    defaultCommunicationMode: 'text',
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      TOOLS.createCollaboration,
      TOOLS.joinCollaboration,
      TOOLS.getCollaborationContext,
      TOOLS.writeToCollaboration,
      TOOLS.closeCollaboration,
      TOOLS.listMyCollaborations,
      TOOLS.broadcastMessage,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'MessageSquareShare',
    },
  },
  [AGENT_TYPES.MERGER]: {
    id: AGENT_TYPES.MERGER,
    name: 'Structural Merger',
    systemPrompt: MERGER_SYSTEM_PROMPT,
    description: 'Reconciliation node. Resolves semantic conflicts between parallel code changes.',
    category: AgentCategory.SYSTEM,
    icon: 'GitMerge',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.THINKING,
    defaultCommunicationMode: 'json',
    tools: [
      ...UNIVERSAL_SYSTEM_TOOLS,
      ...DEVELOPER_TOOLS,
      TOOLS.technicalResearch,
      TOOLS.requestResearch,
    ],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.STORAGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'GitMerge',
    },
  },
  // Handlers (Logic-only, but registered for topology awareness)
  [AGENT_TYPES.BUILD_MONITOR]: {
    id: AGENT_TYPES.BUILD_MONITOR,
    name: 'Build Monitor',
    agentType: 'logic',
    description: 'Observability node. Watches builds, updates gaps, and discovers infra.',
    category: AgentCategory.SYSTEM,
    icon: 'Activity',
    enabled: true,
    ...BACKBONE_HANDLER_DEFAULTS,
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.CONFIG,
      ConnectionProfile.CODEBUILD,
      ConnectionProfile.MEMORY,
    ],
    topologyOverride: {
      tier: 'UTILITY',
      icon: 'Activity',
    },
  },
  [AGENT_TYPES.RECOVERY]: {
    id: AGENT_TYPES.RECOVERY,
    name: "Dead Man's Switch",
    agentType: 'logic',
    description: 'Resilience node. Performs health probes and emergency git-reverts.',
    category: AgentCategory.SYSTEM,
    icon: 'ShieldCheck',
    enabled: true,
    ...BACKBONE_HANDLER_DEFAULTS,
    connectionProfile: [ConnectionProfile.DEPLOYER, ConnectionProfile.MEMORY_TABLE],
    topologyOverride: {
      tier: 'UTILITY',
      icon: 'ShieldCheck',
    },
  },
  [AGENT_TYPES.RESEARCHER]: {
    id: AGENT_TYPES.RESEARCHER,
    name: 'Researcher',
    systemPrompt: RESEARCHER_SYSTEM_PROMPT,
    description: 'Technical discovery node. Researches libraries, frameworks, and patterns.',
    category: AgentCategory.SYSTEM,
    icon: 'Microscope',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.THINKING,
    defaultCommunicationMode: 'text',
    tools: [...UNIVERSAL_SYSTEM_TOOLS, ...WEB_TOOLS, TOOLS.reportGap],
    connectionProfile: [
      ConnectionProfile.BUS,
      ConnectionProfile.MEMORY,
      ConnectionProfile.CONFIG,
      ConnectionProfile.TRACE,
      ConnectionProfile.KNOWLEDGE,
    ],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Microscope',
    },
  },
  [AGENT_TYPES.EVENT_HANDLER]: {
    id: AGENT_TYPES.EVENT_HANDLER,
    name: 'Event Handler',
    agentType: 'logic',
    description: 'Event processing handler. Routes and processes EventBridge events.',
    category: AgentCategory.SYSTEM,
    icon: 'Zap',
    enabled: true,
    ...BACKBONE_HANDLER_DEFAULTS,
    connectionProfile: [ConnectionProfile.MEMORY],
    topologyOverride: {
      tier: 'UTILITY',
      icon: 'Zap',
    },
  },
  [AGENT_TYPES.JUDGE]: {
    id: AGENT_TYPES.JUDGE,
    name: 'Judge Agent',
    systemPrompt: JUDGE_SYSTEM_PROMPT,
    description:
      'Impartial evaluator. Provides objective semantic verification and trust calibration.',
    category: AgentCategory.SYSTEM,
    icon: 'Gavel',
    enabled: true,
    ...BACKBONE_LLM_DEFAULTS,
    ...BACKBONE_LLM_CONFIG,
    reasoningProfile: ReasoningProfile.THINKING,
    defaultCommunicationMode: 'json',
    tools: [...UNIVERSAL_SYSTEM_TOOLS],
    connectionProfile: [ConnectionProfile.MEMORY, ConnectionProfile.TRACE],
    topologyOverride: {
      tier: 'AGENT',
      icon: 'Gavel',
    },
  },
};
