/**
 * Built-in Group Commands
 *
 * Commands for node grouping:
 * - groupNodes: Group selected nodes into a container
 * - ungroupNodes: Remove grouping from a group node
 * - collapseGroup: Collapse a group
 * - expandGroup: Expand a group
 */

import type { CommandDefinition } from '../types';
import { registerCommand } from '../registry';

/**
 * Group selected nodes
 */
export const groupNodesCommand: CommandDefinition = {
	name: 'groupNodes',
	description: 'Group selected nodes into a container',
	aliases: ['group'],
	category: 'nodes',
	inputs: [
		{
			name: 'label',
			type: 'text',
			prompt: 'Group label:',
			required: false,
			default: 'Group',
		},
	],
	execute: async (inputs, ctx) => {
		if (ctx.selectedNodeIds.size < 2) return;

		const { addNodeToLocalGraphAtom } = await import('../../core/graph-mutations');
		const { groupSelectedNodesAtom } = await import('../../core/group-store');
		const label = (inputs.label as string) || 'Group';
		const groupNodeId = crypto.randomUUID();

		// Create the group node in the graph
		ctx.set(addNodeToLocalGraphAtom, {
			id: groupNodeId,
			graph_id: ctx.currentGraphId || '',
			label,
			node_type: 'group',
			configuration: null,
			ui_properties: { x: 0, y: 0, width: 500, height: 500, size: 15, zIndex: 0 },
			data: null,
			created_at: new Date().toISOString(),
			updated_at: new Date().toISOString(),
		});

		// Set parent relationships and auto-size
		ctx.set(groupSelectedNodesAtom, {
			nodeIds: Array.from(ctx.selectedNodeIds),
			groupNodeId,
		});
	},
};

/**
 * Ungroup nodes from a group
 */
export const ungroupNodesCommand: CommandDefinition = {
	name: 'ungroupNodes',
	description: 'Remove grouping from a group node',
	aliases: ['ungroup'],
	category: 'nodes',
	inputs: [
		{
			name: 'groupNode',
			type: 'node',
			prompt: 'Pick a group node to ungroup:',
		},
	],
	execute: async (inputs, ctx) => {
		const groupId = inputs.groupNode as string;
		if (!groupId) return;

		const { ungroupNodesAtom } = await import('../../core/group-store');
		ctx.set(ungroupNodesAtom, groupId);
	},
};

/**
 * Collapse a group
 */
export const collapseGroupCommand: CommandDefinition = {
	name: 'collapseGroup',
	description: 'Collapse a group node to hide its children',
	aliases: ['collapse'],
	category: 'nodes',
	inputs: [
		{
			name: 'groupNode',
			type: 'node',
			prompt: 'Pick a group node to collapse:',
		},
	],
	execute: async (inputs, ctx) => {
		const groupId = inputs.groupNode as string;
		if (!groupId) return;

		const { collapseGroupAtom } = await import('../../core/group-store');
		ctx.set(collapseGroupAtom, groupId);
	},
};

/**
 * Expand a group
 */
export const expandGroupCommand: CommandDefinition = {
	name: 'expandGroup',
	description: 'Expand a collapsed group node',
	aliases: ['expand'],
	category: 'nodes',
	inputs: [
		{
			name: 'groupNode',
			type: 'node',
			prompt: 'Pick a group node to expand:',
		},
	],
	execute: async (inputs, ctx) => {
		const groupId = inputs.groupNode as string;
		if (!groupId) return;

		const { expandGroupAtom } = await import('../../core/group-store');
		ctx.set(expandGroupAtom, groupId);
	},
};

/**
 * Register all group commands
 */
export function registerGroupCommands(): void {
	registerCommand(groupNodesCommand);
	registerCommand(ungroupNodesCommand);
	registerCommand(collapseGroupCommand);
	registerCommand(expandGroupCommand);
}
