import { memo } from 'react';
import NodeShell from '../components/NodeShell.jsx';
import { NodeDraftInput, NodeDraftTextarea } from '../components/NodeDraftField.jsx';
import { useWorkflow } from '../context/WorkflowContext.jsx';
import { getNodeAccent } from '../lib/nodeRegistry.js';

function SendWebSocketFlowNode({ id, data, selected, type }) {
  const { getNodeHandles, patchNodeData } = useWorkflow();
  const handles = getNodeHandles(type, data);
  const runtime = data.runtime || {};

  return (
    <NodeShell
      nodeId={id}
      title={data.title}
      accent={getNodeAccent(type)}
      selected={selected}
      status={runtime.status}
      inputs={handles.inputs}
      outputs={handles.outputs}
    >
      <div className="field-stack">
        <label className="field-stack">
          <span className="field-label">WebSocket URL</span>
          <NodeDraftInput
            className="nodrag node-input"
            value={data.url || ''}
            placeholder="ws://localhost:8765"
            onCommit={(value) => patchNodeData(id, { url: value })}
          />
        </label>

        <label className="field-stack">
          <span className="field-label">Тело сообщения</span>
          <NodeDraftTextarea
            className="nodrag nowheel node-textarea"
            value={data.messageTemplate || ''}
            placeholder='{"command":"{text}"}'
            style={{ minHeight: '104px' }}
            onCommit={(value) => patchNodeData(id, { messageTemplate: value })}
          />
        </label>

        <div className="node-note">
          {runtime.message ? `Отправлено в ${runtime.url}` : 'Используйте {text} для входной строки. Значения памяти вроде {name} тоже поддерживаются в interactive run.'}
        </div>

        {runtime.error ? <div className="node-error">{runtime.error}</div> : null}
      </div>
    </NodeShell>
  );
}

export default memo(SendWebSocketFlowNode);
