import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import { ReactFlowProvider } from 'reactflow';
import AnnotationEdge from './AnnotationEdge';

const EdgeSvgWrapper = (Story: React.ComponentType) => (
  <ReactFlowProvider>
    <div style={{ width: 500, height: 300, position: 'relative', background: '#f5f5f5', borderRadius: 8 }}>
      <svg width="500" height="300" style={{ position: 'absolute', top: 0, left: 0 }}>
        <Story />
      </svg>
    </div>
  </ReactFlowProvider>
);

const meta: Meta<typeof AnnotationEdge> = {
  title: 'Components/Edges/AnnotationEdge',
  component: AnnotationEdge,
  decorators: [EdgeSvgWrapper],
  parameters: {
    layout: 'centered',
  },
  args: {
    id: 'annotation_edge_1',
    source: 'node_1',
    target: 'node_2',
    sourceX: 50,
    sourceY: 50,
    targetX: 400,
    targetY: 200,
    sourcePosition: 'bottom' as any,
    targetPosition: 'top' as any,
    selected: false,
    data: {
      annotation: 'This edge connects the event to the first action.',
      isAnnotationVisible: true,
      isLocked: false,
      locked: false,
      edgeOrdersVisible: false,
      onEdgeUpdate: fn(),
      onToggleAnnotation: fn(),
      onReorderEdge: fn(),
    },
  },
  argTypes: {
    selected: {
      control: 'boolean',
      description: 'Whether the edge is selected',
    },
    'data.annotation': {
      control: 'text',
      description: 'Annotation text displayed on the edge',
    },
    'data.isAnnotationVisible': {
      control: 'boolean',
      description: 'Whether the annotation is visible',
    },
    'data.isLocked': {
      control: 'boolean',
      description: 'Whether the edge is locked',
    },
  },
};

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

/**
 * Default annotation edge with visible annotation
 */
export const Default: Story = {};

/**
 * Selected annotation edge
 */
export const Selected: Story = {
  args: {
    selected: true,
  },
};

/**
 * Annotation edge with hidden annotation
 */
export const AnnotationHidden: Story = {
  args: {
    data: {
      annotation: 'This annotation is currently hidden.',
      isAnnotationVisible: false,
      onEdgeUpdate: fn(),
      onToggleAnnotation: fn(),
      onReorderEdge: fn(),
    },
  },
};

/**
 * Locked annotation edge
 */
export const Locked: Story = {
  args: {
    data: {
      annotation: 'Protected connection annotation.',
      isAnnotationVisible: true,
      isLocked: true,
      locked: true,
      onEdgeUpdate: fn(),
      onToggleAnnotation: fn(),
      onReorderEdge: fn(),
    },
  },
};

/**
 * Long annotation text
 */
export const LongAnnotation: Story = {
  args: {
    data: {
      annotation: 'This is a very detailed annotation that explains the purpose of this connection between the two workflow nodes. It may wrap across multiple lines in the display.',
      isAnnotationVisible: true,
      onEdgeUpdate: fn(),
      onToggleAnnotation: fn(),
      onReorderEdge: fn(),
    },
  },
};
