/**
 * Trustee node config — featureInstanceId, optional SharePoint connection + folder, prompt.
 * Covers: trustee.extractFromFiles, trustee.processDocuments, trustee.syncToAccounting.
 */

import React, { useEffect, useState } from 'react';
import type { NodeConfigRendererProps } from './types';
import { fetchConnections, type UserConnection } from '../../../../api/workflowApi';

import { useLanguage } from '../../../../providers/language/LanguageContext';

export const TrusteeNodeConfig: React.FC<NodeConfigRendererProps> = ({ params,
  updateParam,
  instanceId,
  request,
  nodeType = 'trustee.extractFromFiles',
}) => {
  const { t } = useLanguage();
  const [connections, setConnections] = useState<UserConnection[]>([]);
  const [loading, setLoading] = useState(false);

  const isExtract = nodeType === 'trustee.extractFromFiles';

  useEffect(() => {
    if (isExtract && instanceId && request) {
      setLoading(true);
      fetchConnections(request, instanceId)
        .then(setConnections)
        .catch(() => setConnections([]))
        .finally(() => setLoading(false));
    }
  }, [isExtract, instanceId, request]);

  return (
    <>
      <div>
        <label>{t('trusteeNodeConfig.trusteeInstanceId')}</label>
        <input
          value={(params.featureInstanceId as string) ?? ''}
          onChange={(e) => updateParam('featureInstanceId', e.target.value)}
          placeholder={t('trusteeNodeConfig.trusteeFeatureinstanzid')}
        />
      </div>

      {isExtract && (
        <>
          <div>
            <label>{t('trusteeNodeConfig.sharepointConnectionOptional')}</label>
            <select
              value={(params.connectionId as string) ?? ''}
              onChange={(e) => updateParam('connectionId', e.target.value)}
              disabled={loading}
            >
              <option value="">{loading ? t('trusteeNodeConfig.laden') : t('trusteeNodeConfig.keineDateienAusVorherigemSchritt')}</option>
              {connections.map((c) => (
                <option key={c.id} value={c.id}>
                  {c.externalUsername ?? c.id}
                </option>
              ))}
            </select>
          </div>
          <div>
            <label>{t('trusteeNodeConfig.sharepointOrdnerpfadOptional')}</label>
            <input
              value={(params.sharepointFolder as string) ?? ''}
              onChange={(e) => updateParam('sharepointFolder', e.target.value)}
              placeholder="/sites/MySite/Documents/Expenses"
            />
          </div>
          <div>
            <label>{t('trusteeNodeConfig.aiPromptOptional')}</label>
            <textarea
              value={(params.prompt as string) ?? ''}
              onChange={(e) => updateParam('prompt', e.target.value)}
              placeholder={t('trusteeNodeConfig.zusaetzlicheAnweisungenFuerDieAiextraktion')}
              rows={3}
            />
          </div>
        </>
      )}

      {!isExtract && (
        <div>
          <label>{t('trusteeNodeConfig.documentListReferenz')}</label>
          <input
            value={(params.documentList as string) ?? ''}
            onChange={(e) => updateParam('documentList', e.target.value)}
            placeholder={t('trusteeNodeConfig.referenzAufVorherigenSchrittAutomatisch')}
          />
        </div>
      )}
    </>
  );
};
