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

export function WorkspaceForm({ config, onChange, overriddenByEnv }: SectionFormProps) {
  return (
    <div className="space-y-5">
      <h2 className="text-base font-semibold text-slate-800">Workspace</h2>

      <div>
        <FieldLabel>Worktree Directory</FieldLabel>
        <FieldInput
          value={config.worktreeDir ?? ''}
          onChange={v => onChange('worktreeDir', v)}
          disabled={!!overriddenByEnv['worktreeDir']}
          disabledReason="WORKTREE_DIR 環境変数で上書き中"
        />
        {overriddenByEnv['worktreeDir'] && <EnvOverrideWarning />}
        <HelpText>ジョブ実行時の作業ディレクトリのベースパス</HelpText>
      </div>

      <div>
        <FieldLabel>Custom Pieces Directory</FieldLabel>
        <FieldInput value={config.customPiecesDir ?? ''} onChange={v => onChange('customPiecesDir', v || undefined)}
          placeholder="/path/to/your/custom-pieces" />
        <HelpText>リポジトリ内の pieces/ とは別に、追加の Piece を配置するディレクトリ。省略時は pieces/ のみ使用</HelpText>
      </div>

      <div>
        <FieldLabel>Concurrency</FieldLabel>
        <FieldInput
          type="number"
          value={config.concurrency ?? ''}
          onChange={v => onChange('concurrency', v ? Number(v) : undefined)}
          disabled={!!overriddenByEnv['concurrency']}
          disabledReason="CONCURRENCY 環境変数で上書き中"
        />
        {overriddenByEnv['concurrency'] && <EnvOverrideWarning />}
        <HelpText>同時実行可能なジョブ数</HelpText>
      </div>

      <div>
        <FieldLabel>Max Movements</FieldLabel>
        <FieldInput type="number" value={config.maxMovements ?? ''} onChange={v => onChange('maxMovements', v ? Number(v) : undefined)} />
        <HelpText>1ジョブあたりの最大 movement 数</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>ジョブ失敗時の最大リトライ回数。デフォルト: 3</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>リトライ間隔（秒）。カンマ区切り。デフォルト: 60, 300, 900</HelpText>
      </div>
    </div>
  );
}
