import { useTranslation } from 'react-i18next';
import { HelpText } from './HelpText';
import { EnvOverrideWarning, FieldLabel, FieldInput } from './formUtils';
import type { SectionFormProps } from './types';

/**
 * Execution — concurrency, max_movements, and job retry settings.
 *
 * Step 3 carve-out from the old "Workspace" form. The path/storage half
 * of Workspace lives in PathsStorageForm. Field paths are unchanged so
 * the underlying config keys keep working without a migration.
 */
export function ExecutionForm({ config, onChange, overriddenByEnv }: SectionFormProps) {
  const { t } = useTranslation('settings');
  return (
    <div className="space-y-5">
      <h2 className="text-base font-semibold text-slate-800">Execution</h2>
      <HelpText>{t('execution.intro')}</HelpText>

      <div>
        <FieldLabel>Concurrency</FieldLabel>
        <FieldInput
          type="number"
          value={config.concurrency ?? ''}
          onChange={v => onChange('concurrency', v ? Number(v) : undefined)}
          disabled={!!overriddenByEnv['concurrency']}
          disabledReason={t('execution.concurrencyOverride')}
        />
        {overriddenByEnv['concurrency'] && <EnvOverrideWarning />}
        <HelpText>{t('execution.concurrencyHelp')}</HelpText>
      </div>

      <div>
        <FieldLabel>Max Movements</FieldLabel>
        <FieldInput
          type="number"
          value={config.maxMovements ?? ''}
          onChange={v => onChange('maxMovements', v ? Number(v) : undefined)}
        />
        <HelpText>{t('execution.maxMovementsHelp')}</HelpText>
      </div>

      <h3 className="text-sm font-medium text-slate-600 mt-4 pt-3 border-t border-slate-200">Retry</h3>

      <div>
        <FieldLabel>Max Attempts</FieldLabel>
        <FieldInput
          type="number"
          value={config.retry?.maxAttempts ?? 3}
          onChange={v => onChange('retry.maxAttempts', Number(v))}
        />
        <HelpText>{t('execution.maxAttemptsHelp')}</HelpText>
      </div>

      <div>
        <FieldLabel>Backoff Seconds</FieldLabel>
        <FieldInput
          value={(config.retry?.backoffSeconds ?? [60, 300, 900]).join(', ')}
          onChange={v =>
            onChange(
              'retry.backoffSeconds',
              v.split(',').map((s: string) => Number(s.trim())).filter((n: number) => !isNaN(n)),
            )
          }
        />
        <HelpText>{t('execution.backoffHelp')}</HelpText>
      </div>
    </div>
  );
}
