/**
 * AeThex Visual Scripting - Node Definitions
 * All available nodes for the visual scripting system
 */

import { Node, Edge } from 'reactflow';

// Node Categories
export type NodeCategory =
  | 'events'      // Entry points (green)
  | 'logic'       // Control flow (blue)
  | 'actions'     // Do things (purple)
  | 'data'        // Values and operations (orange)
  | 'references'  // Game objects (yellow)
  | 'custom';     // User-defined (gray)

// Port types for connections
export type PortType =
  | 'flow'        // Execution flow (white)
  | 'number'      // Number values (blue)
  | 'string'      // Text values (pink)
  | 'boolean'     // True/false (red)
  | 'object'      // Game objects (yellow)
  | 'array'       // Lists (green)
  | 'any';        // Any type (gray)

export interface NodePort {
  id: string;
  name: string;
  type: PortType;
  defaultValue?: any;
  required?: boolean;
}

export interface NodeDefinition {
  type: string;
  category: NodeCategory;
  label: string;
  description: string;
  icon: string;
  color: string;
  inputs: NodePort[];
  outputs: NodePort[];
  platforms: ('roblox' | 'uefn' | 'spatial')[];
  codeTemplate: {
    roblox?: string;
    uefn?: string;
    spatial?: string;
  };
}

// Color mapping for categories
export const CATEGORY_COLORS: Record<NodeCategory, string> = {
  events: '#22c55e',      // Green
  logic: '#3b82f6',       // Blue
  actions: '#a855f7',     // Purple
  data: '#f97316',        // Orange
  references: '#eab308',  // Yellow
  custom: '#6b7280',      // Gray
};

// Color mapping for port types
export const PORT_COLORS: Record<PortType, string> = {
  flow: '#ffffff',
  number: '#3b82f6',
  string: '#ec4899',
  boolean: '#ef4444',
  object: '#eab308',
  array: '#22c55e',
  any: '#6b7280',
};

// ============================================
// EVENT NODES - Entry points for scripts
// ============================================

export const EVENT_NODES: NodeDefinition[] = [
  {
    type: 'onPlayerJoin',
    category: 'events',
    label: 'On Player Join',
    description: 'Triggered when a player joins the game',
    icon: 'UserPlus',
    color: CATEGORY_COLORS.events,
    inputs: [],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'player', name: 'Player', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `game.Players.PlayerAdded:Connect(function(player)\n{{BODY}}\nend)`,
      uefn: `OnPlayerAdded<public>(): void =\n{{BODY}}`,
      spatial: `SpaceService.onPlayerJoined.on((player) => {\n{{BODY}}\n});`,
    },
  },
  {
    type: 'onPlayerLeave',
    category: 'events',
    label: 'On Player Leave',
    description: 'Triggered when a player leaves the game',
    icon: 'UserMinus',
    color: CATEGORY_COLORS.events,
    inputs: [],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'player', name: 'Player', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `game.Players.PlayerRemoving:Connect(function(player)\n{{BODY}}\nend)`,
      uefn: `OnPlayerRemoved<public>(): void =\n{{BODY}}`,
      spatial: `SpaceService.onPlayerLeft.on((player) => {\n{{BODY}}\n});`,
    },
  },
  {
    type: 'onPartTouch',
    category: 'events',
    label: 'On Part Touched',
    description: 'Triggered when something touches a part',
    icon: 'Hand',
    color: CATEGORY_COLORS.events,
    inputs: [
      { id: 'part', name: 'Part', type: 'object', required: true },
    ],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'otherPart', name: 'Other Part', type: 'object' },
      { id: 'player', name: 'Player (if any)', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{part}}.Touched:Connect(function(otherPart)\n\tlocal player = game.Players:GetPlayerFromCharacter(otherPart.Parent)\n{{BODY}}\nend)`,
      uefn: `{{part}}.OnBeginOverlap.Subscribe(function(OtherActor: actor):\n{{BODY}}`,
      spatial: `{{part}}.onCollisionEnter.on((collision) => {\n{{BODY}}\n});`,
    },
  },
  {
    type: 'onKeyPress',
    category: 'events',
    label: 'On Key Press',
    description: 'Triggered when a key is pressed',
    icon: 'Keyboard',
    color: CATEGORY_COLORS.events,
    inputs: [
      { id: 'key', name: 'Key', type: 'string', defaultValue: 'E' },
    ],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'player', name: 'Player', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `local UserInputService = game:GetService("UserInputService")\nUserInputService.InputBegan:Connect(function(input, gameProcessed)\n\tif not gameProcessed and input.KeyCode == Enum.KeyCode.{{key}} then\n{{BODY}}\n\tend\nend)`,
      uefn: `OnKeyPressed(Key: keycode_{{key}}): void =\n{{BODY}}`,
      spatial: `Input.onKeyDown("{{key}}", () => {\n{{BODY}}\n});`,
    },
  },
  {
    type: 'onTimer',
    category: 'events',
    label: 'On Timer',
    description: 'Triggers repeatedly at an interval',
    icon: 'Clock',
    color: CATEGORY_COLORS.events,
    inputs: [
      { id: 'interval', name: 'Interval (sec)', type: 'number', defaultValue: 1 },
    ],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `while true do\n\ttask.wait({{interval}})\n{{BODY}}\nend`,
      uefn: `loop:\n\tSleep({{interval}})\n{{BODY}}`,
      spatial: `setInterval(() => {\n{{BODY}}\n}, {{interval}} * 1000);`,
    },
  },
  {
    type: 'onGameStart',
    category: 'events',
    label: 'On Game Start',
    description: 'Runs once when the game starts',
    icon: 'Play',
    color: CATEGORY_COLORS.events,
    inputs: [],
    outputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `-- Runs on game start\n{{BODY}}`,
      uefn: `OnBegin<override>(): void =\n{{BODY}}`,
      spatial: `SpaceService.onSpaceReady.on(() => {\n{{BODY}}\n});`,
    },
  },
];

// ============================================
// LOGIC NODES - Control flow
// ============================================

export const LOGIC_NODES: NodeDefinition[] = [
  {
    type: 'ifCondition',
    category: 'logic',
    label: 'If',
    description: 'Branch based on a condition',
    icon: 'GitBranch',
    color: CATEGORY_COLORS.logic,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'condition', name: 'Condition', type: 'boolean', required: true },
    ],
    outputs: [
      { id: 'true', name: 'True', type: 'flow' },
      { id: 'false', name: 'False', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `if {{condition}} then\n{{TRUE_BODY}}\nelse\n{{FALSE_BODY}}\nend`,
      uefn: `if ({{condition}}):\n{{TRUE_BODY}}\nelse:\n{{FALSE_BODY}}`,
      spatial: `if ({{condition}}) {\n{{TRUE_BODY}}\n} else {\n{{FALSE_BODY}}\n}`,
    },
  },
  {
    type: 'forLoop',
    category: 'logic',
    label: 'For Loop',
    description: 'Repeat a number of times',
    icon: 'Repeat',
    color: CATEGORY_COLORS.logic,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'start', name: 'Start', type: 'number', defaultValue: 1 },
      { id: 'end', name: 'End', type: 'number', defaultValue: 10 },
    ],
    outputs: [
      { id: 'loop', name: 'Loop Body', type: 'flow' },
      { id: 'index', name: 'Index', type: 'number' },
      { id: 'complete', name: 'Completed', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `for i = {{start}}, {{end}} do\n{{LOOP_BODY}}\nend\n{{COMPLETE_BODY}}`,
      uefn: `for (Index := {{start}}..{{end}}):\n{{LOOP_BODY}}\n{{COMPLETE_BODY}}`,
      spatial: `for (let i = {{start}}; i <= {{end}}; i++) {\n{{LOOP_BODY}}\n}\n{{COMPLETE_BODY}}`,
    },
  },
  {
    type: 'forEach',
    category: 'logic',
    label: 'For Each',
    description: 'Loop through a list',
    icon: 'List',
    color: CATEGORY_COLORS.logic,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'array', name: 'Array', type: 'array', required: true },
    ],
    outputs: [
      { id: 'loop', name: 'Loop Body', type: 'flow' },
      { id: 'item', name: 'Current Item', type: 'any' },
      { id: 'index', name: 'Index', type: 'number' },
      { id: 'complete', name: 'Completed', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `for index, item in ipairs({{array}}) do\n{{LOOP_BODY}}\nend\n{{COMPLETE_BODY}}`,
      uefn: `for (Index -> Item : {{array}}):\n{{LOOP_BODY}}\n{{COMPLETE_BODY}}`,
      spatial: `{{array}}.forEach((item, index) => {\n{{LOOP_BODY}}\n});\n{{COMPLETE_BODY}}`,
    },
  },
  {
    type: 'wait',
    category: 'logic',
    label: 'Wait',
    description: 'Pause execution for a duration',
    icon: 'Clock',
    color: CATEGORY_COLORS.logic,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'duration', name: 'Seconds', type: 'number', defaultValue: 1 },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `task.wait({{duration}})\n{{BODY}}`,
      uefn: `Sleep({{duration}})\n{{BODY}}`,
      spatial: `await delay({{duration}} * 1000);\n{{BODY}}`,
    },
  },
  {
    type: 'whileLoop',
    category: 'logic',
    label: 'While',
    description: 'Loop while condition is true',
    icon: 'RefreshCw',
    color: CATEGORY_COLORS.logic,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'condition', name: 'Condition', type: 'boolean', required: true },
    ],
    outputs: [
      { id: 'loop', name: 'Loop Body', type: 'flow' },
      { id: 'complete', name: 'Completed', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `while {{condition}} do\n{{LOOP_BODY}}\nend\n{{COMPLETE_BODY}}`,
      uefn: `loop:\n\tif (not {{condition}}): break\n{{LOOP_BODY}}`,
      spatial: `while ({{condition}}) {\n{{LOOP_BODY}}\n}\n{{COMPLETE_BODY}}`,
    },
  },
];

// ============================================
// ACTION NODES - Do things
// ============================================

export const ACTION_NODES: NodeDefinition[] = [
  {
    type: 'print',
    category: 'actions',
    label: 'Print',
    description: 'Print a message to the console',
    icon: 'MessageSquare',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'message', name: 'Message', type: 'string', defaultValue: 'Hello!' },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `print({{message}})\n{{BODY}}`,
      uefn: `Print({{message}})\n{{BODY}}`,
      spatial: `console.log({{message}});\n{{BODY}}`,
    },
  },
  {
    type: 'setProperty',
    category: 'actions',
    label: 'Set Property',
    description: 'Set a property on an object',
    icon: 'Settings',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'object', name: 'Object', type: 'object', required: true },
      { id: 'property', name: 'Property', type: 'string', required: true },
      { id: 'value', name: 'Value', type: 'any', required: true },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{object}}.{{property}} = {{value}}\n{{BODY}}`,
      uefn: `set {{object}}.{{property}} = {{value}}\n{{BODY}}`,
      spatial: `{{object}}.{{property}} = {{value}};\n{{BODY}}`,
    },
  },
  {
    type: 'createPart',
    category: 'actions',
    label: 'Create Part',
    description: 'Create a new part in the world',
    icon: 'Box',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'position', name: 'Position', type: 'object' },
      { id: 'size', name: 'Size', type: 'object' },
      { id: 'color', name: 'Color', type: 'object' },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
      { id: 'part', name: 'Created Part', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `local newPart = Instance.new("Part")\nnewPart.Position = {{position}}\nnewPart.Size = {{size}}\nnewPart.BrickColor = {{color}}\nnewPart.Parent = workspace\n{{BODY}}`,
      uefn: `var NewProp := SpawnProp(DefaultProp)\nNewProp.SetTransform({{position}})\n{{BODY}}`,
      spatial: `const newPart = createPart({{position}}, {{size}}, {{color}});\n{{BODY}}`,
    },
  },
  {
    type: 'destroy',
    category: 'actions',
    label: 'Destroy',
    description: 'Destroy an object',
    icon: 'Trash2',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'object', name: 'Object', type: 'object', required: true },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{object}}:Destroy()\n{{BODY}}`,
      uefn: `{{object}}.Dispose()\n{{BODY}}`,
      spatial: `{{object}}.destroy();\n{{BODY}}`,
    },
  },
  {
    type: 'playSound',
    category: 'actions',
    label: 'Play Sound',
    description: 'Play a sound effect',
    icon: 'Volume2',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'soundId', name: 'Sound ID', type: 'string', required: true },
      { id: 'volume', name: 'Volume', type: 'number', defaultValue: 1 },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `local sound = Instance.new("Sound")\nsound.SoundId = "rbxassetid://{{soundId}}"\nsound.Volume = {{volume}}\nsound.Parent = workspace\nsound:Play()\n{{BODY}}`,
      uefn: `PlaySound({{soundId}}, {{volume}})\n{{BODY}}`,
      spatial: `playSound("{{soundId}}", { volume: {{volume}} });\n{{BODY}}`,
    },
  },
  {
    type: 'tween',
    category: 'actions',
    label: 'Tween Property',
    description: 'Smoothly animate a property',
    icon: 'Sparkles',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'object', name: 'Object', type: 'object', required: true },
      { id: 'property', name: 'Property', type: 'string', required: true },
      { id: 'target', name: 'Target Value', type: 'any', required: true },
      { id: 'duration', name: 'Duration', type: 'number', defaultValue: 1 },
    ],
    outputs: [
      { id: 'flow', name: 'On Complete', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `local TweenService = game:GetService("TweenService")\nlocal tween = TweenService:Create({{object}}, TweenInfo.new({{duration}}), {{{property}} = {{target}}})\ntween:Play()\ntween.Completed:Wait()\n{{BODY}}`,
      uefn: `{{object}}.MoveAndRotateTo({{target}}, {{duration}})\n{{BODY}}`,
      spatial: `animate({{object}}, { {{property}}: {{target}} }, {{duration}} * 1000).then(() => {\n{{BODY}}\n});`,
    },
  },
  {
    type: 'teleport',
    category: 'actions',
    label: 'Teleport Player',
    description: 'Move a player to a position',
    icon: 'Zap',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'player', name: 'Player', type: 'object', required: true },
      { id: 'position', name: 'Position', type: 'object', required: true },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{player}}.Character:SetPrimaryPartCFrame(CFrame.new({{position}}))\n{{BODY}}`,
      uefn: `{{player}}.Respawn({{position}}, {{player}}.GetRotation())\n{{BODY}}`,
      spatial: `{{player}}.teleportTo({{position}});\n{{BODY}}`,
    },
  },
  {
    type: 'giveItem',
    category: 'actions',
    label: 'Give Item',
    description: 'Give an item/tool to a player',
    icon: 'Gift',
    color: CATEGORY_COLORS.actions,
    inputs: [
      { id: 'flow', name: 'Execute', type: 'flow' },
      { id: 'player', name: 'Player', type: 'object', required: true },
      { id: 'item', name: 'Item', type: 'object', required: true },
    ],
    outputs: [
      { id: 'flow', name: 'Continue', type: 'flow' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `local itemClone = {{item}}:Clone()\nitemClone.Parent = {{player}}.Backpack\n{{BODY}}`,
      uefn: `GrantItem({{player}}, {{item}})\n{{BODY}}`,
      spatial: `{{player}}.inventory.add({{item}});\n{{BODY}}`,
    },
  },
];

// ============================================
// DATA NODES - Values and operations
// ============================================

export const DATA_NODES: NodeDefinition[] = [
  {
    type: 'number',
    category: 'data',
    label: 'Number',
    description: 'A number value',
    icon: 'Hash',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'value', name: 'Value', type: 'number', defaultValue: 0 },
    ],
    outputs: [
      { id: 'value', name: 'Value', type: 'number' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{value}}`,
      uefn: `{{value}}`,
      spatial: `{{value}}`,
    },
  },
  {
    type: 'string',
    category: 'data',
    label: 'Text',
    description: 'A text value',
    icon: 'Type',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'value', name: 'Value', type: 'string', defaultValue: '' },
    ],
    outputs: [
      { id: 'value', name: 'Value', type: 'string' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `"{{value}}"`,
      uefn: `"{{value}}"`,
      spatial: `"{{value}}"`,
    },
  },
  {
    type: 'boolean',
    category: 'data',
    label: 'Boolean',
    description: 'True or False',
    icon: 'ToggleLeft',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'value', name: 'Value', type: 'boolean', defaultValue: true },
    ],
    outputs: [
      { id: 'value', name: 'Value', type: 'boolean' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{value}}`,
      uefn: `{{value}}`,
      spatial: `{{value}}`,
    },
  },
  {
    type: 'vector3',
    category: 'data',
    label: 'Vector3',
    description: '3D position/direction',
    icon: 'Move3d',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'x', name: 'X', type: 'number', defaultValue: 0 },
      { id: 'y', name: 'Y', type: 'number', defaultValue: 0 },
      { id: 'z', name: 'Z', type: 'number', defaultValue: 0 },
    ],
    outputs: [
      { id: 'vector', name: 'Vector', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `Vector3.new({{x}}, {{y}}, {{z}})`,
      uefn: `vector3{X := {{x}}, Y := {{y}}, Z := {{z}}}`,
      spatial: `new Vector3({{x}}, {{y}}, {{z}})`,
    },
  },
  {
    type: 'color',
    category: 'data',
    label: 'Color',
    description: 'RGB color value',
    icon: 'Palette',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'r', name: 'Red', type: 'number', defaultValue: 255 },
      { id: 'g', name: 'Green', type: 'number', defaultValue: 255 },
      { id: 'b', name: 'Blue', type: 'number', defaultValue: 255 },
    ],
    outputs: [
      { id: 'color', name: 'Color', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `Color3.fromRGB({{r}}, {{g}}, {{b}})`,
      uefn: `MakeColor({{r}}, {{g}}, {{b}})`,
      spatial: `new Color({{r}}, {{g}}, {{b}})`,
    },
  },
  {
    type: 'math',
    category: 'data',
    label: 'Math',
    description: 'Mathematical operation',
    icon: 'Calculator',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'a', name: 'A', type: 'number', required: true },
      { id: 'b', name: 'B', type: 'number', required: true },
      { id: 'operation', name: 'Operation', type: 'string', defaultValue: 'add' },
    ],
    outputs: [
      { id: 'result', name: 'Result', type: 'number' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `({{a}} {{OPERATION}} {{b}})`,
      uefn: `({{a}} {{OPERATION}} {{b}})`,
      spatial: `({{a}} {{OPERATION}} {{b}})`,
    },
  },
  {
    type: 'compare',
    category: 'data',
    label: 'Compare',
    description: 'Compare two values',
    icon: 'Scale',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'a', name: 'A', type: 'any', required: true },
      { id: 'b', name: 'B', type: 'any', required: true },
      { id: 'comparison', name: 'Comparison', type: 'string', defaultValue: 'equals' },
    ],
    outputs: [
      { id: 'result', name: 'Result', type: 'boolean' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `({{a}} {{COMPARISON}} {{b}})`,
      uefn: `({{a}} {{COMPARISON}} {{b}})`,
      spatial: `({{a}} {{COMPARISON}} {{b}})`,
    },
  },
  {
    type: 'random',
    category: 'data',
    label: 'Random',
    description: 'Random number in range',
    icon: 'Dices',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'min', name: 'Min', type: 'number', defaultValue: 1 },
      { id: 'max', name: 'Max', type: 'number', defaultValue: 100 },
    ],
    outputs: [
      { id: 'value', name: 'Value', type: 'number' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `math.random({{min}}, {{max}})`,
      uefn: `GetRandomInt({{min}}, {{max}})`,
      spatial: `Math.floor(Math.random() * ({{max}} - {{min}} + 1)) + {{min}}`,
    },
  },
  {
    type: 'variable',
    category: 'data',
    label: 'Variable',
    description: 'Store and retrieve a value',
    icon: 'Variable',
    color: CATEGORY_COLORS.data,
    inputs: [
      { id: 'name', name: 'Name', type: 'string', required: true },
      { id: 'setValue', name: 'Set Value', type: 'any' },
    ],
    outputs: [
      { id: 'getValue', name: 'Get Value', type: 'any' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{name}}`,
      uefn: `{{name}}`,
      spatial: `{{name}}`,
    },
  },
];

// ============================================
// REFERENCE NODES - Game objects
// ============================================

export const REFERENCE_NODES: NodeDefinition[] = [
  {
    type: 'getPlayer',
    category: 'references',
    label: 'Get Player',
    description: 'Get a player by name or index',
    icon: 'User',
    color: CATEGORY_COLORS.references,
    inputs: [
      { id: 'name', name: 'Name (optional)', type: 'string' },
    ],
    outputs: [
      { id: 'player', name: 'Player', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `game.Players:FindFirstChild("{{name}}")`,
      uefn: `GetPlayer[{{name}}]`,
      spatial: `SpaceService.getPlayerByName("{{name}}")`,
    },
  },
  {
    type: 'getAllPlayers',
    category: 'references',
    label: 'Get All Players',
    description: 'Get list of all players',
    icon: 'Users',
    color: CATEGORY_COLORS.references,
    inputs: [],
    outputs: [
      { id: 'players', name: 'Players', type: 'array' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `game.Players:GetPlayers()`,
      uefn: `GetPlayspace().GetPlayers()`,
      spatial: `SpaceService.getAllPlayers()`,
    },
  },
  {
    type: 'findChild',
    category: 'references',
    label: 'Find Child',
    description: 'Find a child object by name',
    icon: 'Search',
    color: CATEGORY_COLORS.references,
    inputs: [
      { id: 'parent', name: 'Parent', type: 'object', required: true },
      { id: 'name', name: 'Name', type: 'string', required: true },
    ],
    outputs: [
      { id: 'child', name: 'Child', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `{{parent}}:FindFirstChild("{{name}}")`,
      uefn: `{{parent}}.GetChildren().Find("{{name}}")`,
      spatial: `{{parent}}.getChildByName("{{name}}")`,
    },
  },
  {
    type: 'workspace',
    category: 'references',
    label: 'Workspace',
    description: 'The game workspace/world',
    icon: 'Globe',
    color: CATEGORY_COLORS.references,
    inputs: [],
    outputs: [
      { id: 'workspace', name: 'Workspace', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `workspace`,
      uefn: `GetPlayspace()`,
      spatial: `World`,
    },
  },
  {
    type: 'getService',
    category: 'references',
    label: 'Get Service',
    description: 'Get a game service',
    icon: 'Server',
    color: CATEGORY_COLORS.references,
    inputs: [
      { id: 'service', name: 'Service Name', type: 'string', required: true },
    ],
    outputs: [
      { id: 'service', name: 'Service', type: 'object' },
    ],
    platforms: ['roblox', 'uefn', 'spatial'],
    codeTemplate: {
      roblox: `game:GetService("{{service}}")`,
      uefn: `Get{{service}}Service()`,
      spatial: `{{service}}Service`,
    },
  },
];

// Combine all nodes
export const ALL_NODES: NodeDefinition[] = [
  ...EVENT_NODES,
  ...LOGIC_NODES,
  ...ACTION_NODES,
  ...DATA_NODES,
  ...REFERENCE_NODES,
];

// Get nodes by category
export function getNodesByCategory(category: NodeCategory): NodeDefinition[] {
  return ALL_NODES.filter(n => n.category === category);
}

// Get node by type
export function getNodeDefinition(type: string): NodeDefinition | undefined {
  return ALL_NODES.find(n => n.type === type);
}

// Get nodes for a specific platform
export function getNodesForPlatform(platform: 'roblox' | 'uefn' | 'spatial'): NodeDefinition[] {
  return ALL_NODES.filter(n => n.platforms.includes(platform));
}
