/**
 * Registers built-in node types (config, render, variable, function).
 * Import this once at app startup (e.g. in main.tsx) so the registry is populated.
 */

import React from 'react'
import { ScrollText, Sparkles, Variable, Code2, Database, Bot } from 'lucide-react'
import { registerNodeType } from './nodeRegistry'
import { NODE_HELP } from './nodeHelp'
import ConfigNode from '../components/nodes/ConfigNode'
import RenderingNode from '../components/nodes/RenderingNode'
import VariableNode from '../components/nodes/VariableNode'
import FunctionNode from '../components/nodes/FunctionNode'
import DataNode from '../components/nodes/DataNode'
import AgentNode from '../components/nodes/AgentNode'

const ICON_CLASS = 'mr-2 h-4 w-4'

export function registerBuiltinNodes(): void {
  registerNodeType({
    id: 'config',
    component: ConfigNode,
    defaultStyle: { width: 320, height: 320 },
    defaultData: { configType: 'plantuml', content: '@startuml\n\n@enduml\n', title: '' },
    idPrefix: 'cfg_',
    hasInput: true,
    hasOutput: true,
    classification: 'psyche',
    allowedSourceTypes: ['config', 'variable', 'function', 'data'],
    allowedTargetTypes: ['config', 'render', 'agent'],
    help: NODE_HELP.config,
    menuLabel: 'Config',
    menuIcon: <ScrollText className={ICON_CLASS} />,
    getDefaultData: (newId) => ({
      configType: 'plantuml',
      content: '@startuml\n\n@enduml\n',
      title: newId ?? '',
    }),
    getResetData: (nodeId) => ({
      configType: 'plantuml',
      content: '@startuml\n\n@enduml\n',
      title: nodeId ?? '',
    }),
    connectionLabel: 'adding input',
    supportsFullscreen: true,
  })

  registerNodeType({
    id: 'render',
    component: RenderingNode,
    defaultStyle: { width: 384, height: 320 },
    defaultData: { viewportWidth: 1200, viewportHeight: 800 },
    idPrefix: 'rnd_',
    hasInput: true,
    hasOutput: false,
    classification: 'pneuma',
    allowedSourceTypes: ['config', 'agent'],
    help: NODE_HELP.render,
    menuLabel: 'Renderer',
    menuIcon: <Sparkles className={ICON_CLASS} />,
    connectionLabel: 'rendering',
    supportsFullscreen: true,
  })

  registerNodeType({
    id: 'agent',
    component: AgentNode,
    defaultStyle: { width: 360, height: 320 },
    defaultData: { context: '' },
    idPrefix: 'agt_',
    hasInput: true,
    hasOutput: true,
    classification: 'archon',
    allowedSourceTypes: ['config', 'variable', 'data'],
    allowedTargetTypes: ['render'],
    help: NODE_HELP.agent,
    menuLabel: 'Agent',
    menuIcon: <Bot className={ICON_CLASS} />,
    connectionLabel: 'prompt/context',
    supportsFullscreen: true,
  })

  registerNodeType({
    id: 'variable',
    component: VariableNode,
    defaultStyle: { width: 224, height: 240 },
    defaultData: { value: '', valueType: 'string' },
    idPrefix: 'var_',
    hasInput: false,
    hasOutput: true,
    classification: 'psyche',
    allowedTargetTypes: ['config', 'function', 'agent'],
    help: NODE_HELP.variable,
    menuLabel: 'Variable',
    menuIcon: <Variable className={ICON_CLASS} />,
  })

  registerNodeType({
    id: 'data',
    component: DataNode,
    defaultStyle: { width: 360, height: 280 },
    defaultData: { rows: [], columns: [], fileName: '' },
    idPrefix: 'data_',
    hasInput: false,
    hasOutput: true,
    classification: 'physis',
    allowedTargetTypes: ['config', 'agent'],
    help: NODE_HELP.data,
    menuLabel: 'Data',
    menuIcon: <Database className={ICON_CLASS} />,
    connectionLabel: 'data source',
  })

  registerNodeType({
    id: 'function',
    component: FunctionNode,
    defaultStyle: { width: 288, height: 260 },
    defaultData: {
      body: `function(num, kwargs) {
  return num + (kwargs.bar || 0);
}`,
    },
    idPrefix: 'fn_',
    hasInput: true,
    hasOutput: true,
    classification: 'psyche',
    allowedSourceTypes: ['config', 'variable', 'function'],
    allowedTargetTypes: ['config', 'function'],
    help: NODE_HELP.function,
    menuLabel: 'Function',
    menuIcon: <Code2 className={ICON_CLASS} />,
    getResetData: () => ({ body: '' }),
    connectionLabel: 'adding input',
    supportsFullscreen: true,
  })
}
