import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import SubprocessNode from './SubprocessNode';
import { withNodeContext } from '../../../.storybook/decorators';

const meta: Meta<typeof SubprocessNode> = {
  title: 'Components/Nodes/SubprocessNode',
  component: SubprocessNode,
  decorators: [withNodeContext],
  parameters: {
    layout: 'centered',
  },
  args: {
    id: 'subprocess_1',
    type: 'subprocess',
    selected: false,
    dragging: false,
    isConnectable: true,
    xPos: 0,
    yPos: 0,
    zIndex: 0,
    data: {
      label: 'Process Order',
      plugin: 'eca_subprocess:execute',
      locked: false,
      annotation: '',
      isAnnotationVisible: false,
      isLocked: false,
      subflowCount: 3,
      onDelete: fn(),
      onToggleAnnotation: fn(),
    },
  },
  argTypes: {
    selected: {
      control: 'boolean',
      description: 'Whether the node is selected',
    },
    'data.label': {
      control: 'text',
      description: 'Node label text',
    },
    'data.plugin': {
      control: 'text',
      description: 'Plugin identifier',
    },
    'data.locked': {
      control: 'boolean',
      description: 'Whether the node is locked',
    },
    'data.subflowCount': {
      control: { type: 'number', min: 0, max: 20 },
      description: 'Number of sub-flows in the subprocess',
    },
    'data.annotation': {
      control: 'text',
      description: 'Annotation text for the node',
    },
    'data.isAnnotationVisible': {
      control: 'boolean',
      description: 'Whether the annotation is visible',
    },
  },
};

export default meta;
type Story = StoryObj<typeof SubprocessNode>;

/**
 * Default subprocess node with sub-flow count
 */
export const Default: Story = {};

/**
 * Selected subprocess node
 */
export const Selected: Story = {
  args: {
    selected: true,
  },
};

/**
 * Locked subprocess node
 */
export const Locked: Story = {
  args: {
    data: {
      label: 'Critical Workflow',
      plugin: 'eca_subprocess:execute',
      locked: true,
      isLocked: true,
      subflowCount: 5,
      onDelete: fn(),
      onToggleAnnotation: fn(),
    },
  },
};

/**
 * Subprocess with visible annotation
 */
export const WithAnnotation: Story = {
  args: {
    data: {
      label: 'Batch Import',
      plugin: 'eca_subprocess:execute',
      subflowCount: 12,
      annotation: 'Handles importing records in batches of 100 to avoid timeouts.',
      isAnnotationVisible: true,
      onDelete: fn(),
      onToggleAnnotation: fn(),
    },
  },
};

/**
 * Subprocess with no sub-flows
 */
export const NoSubflows: Story = {
  args: {
    data: {
      label: 'Empty Subprocess',
      plugin: 'eca_subprocess:execute',
      subflowCount: 0,
      onDelete: fn(),
      onToggleAnnotation: fn(),
    },
  },
};

/**
 * Subprocess node with quick-add button visible (unlocked node with onQuickAdd handler)
 */
export const WithQuickAdd: Story = {
  args: {
    data: {
      label: 'Process Order',
      plugin: 'eca_subprocess:execute',
      locked: false,
      isLocked: false,
      subflowCount: 3,
      onDelete: fn(),
      onToggleAnnotation: fn(),
      onQuickAdd: fn(),
    },
  },
};
