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

function AssistantMessageFlowNode({ 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">
        <NodeDraftTextarea
          className="nodrag nowheel node-textarea node-textarea--inline is-editing"
          value={data.text || ''}
          placeholder="Реплика ассистента..."
          style={{ minHeight: '112px' }}
          onCommit={(value) => patchNodeData(id, { text: value })}
        />

        <label className="field-row">
          <input
            className="nodrag"
            type="checkbox"
            checked={Boolean(data.paraphrase)}
            onChange={(event) => patchNodeData(id, { paraphrase: event.target.checked })}
          />
          <span>Перефразировать при запуске</span>
        </label>

        <div className="node-note">
          {runtime.message ? `Отправлено: ${runtime.message}` : 'Отправляет реплику ассистента в интерактивный transcript.'}
        </div>

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

export default memo(AssistantMessageFlowNode);
