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

/**
 * Legacy Knowledge (DKS) namespace settings.
 *
 * Replaces the `knowledge` tab of the legacy grab-bag `ToolsForm`.
 * The config keys are unchanged:
 *
 *   tools.knowledge_service_url
 *   tools.knowledge_namespaces
 *
 * Marked as legacy in PR #357; new knowledge integrations should go
 * through MCP servers. Existing namespaces remain editable / removable,
 * but adding new namespaces is disabled in the editor.
 */
export function KnowledgeNamespacesForm({ config, onChange }: SectionFormProps) {
  const tools = config.tools ?? {};

  return (
    <div className="space-y-5">
      <div className="flex items-center gap-2">
        <h2 className="text-base font-semibold text-slate-800">Knowledge (DKS)</h2>
        <span
          className="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-semibold uppercase tracking-wide bg-amber-100 text-amber-800 border border-amber-300"
          title="この機能は legacy です。新規の知識検索統合は MCP server 経由を推奨"
        >
          LEGACY
        </span>
      </div>

      <div
        role="note"
        className="rounded border border-amber-300 bg-amber-50 px-3 py-2 text-xs text-amber-900"
      >
        DKS 機能は <strong>legacy</strong> 化されており、新規の知識検索統合は{' '}
        <strong>MCP server 経由</strong> を推奨します。既存の namespace 設定は引き続き動作しますが、
        新規 namespace の追加はできません。{' '}
        <a
          href="/help"
          className="underline text-amber-900 hover:text-amber-700"
          target="_blank"
          rel="noopener noreferrer"
        >
          MCP 連携ガイドを開く
        </a>
      </div>

      <div>
        <FieldLabel>Knowledge Service URL</FieldLabel>
        <FieldInput value={tools.knowledgeServiceUrl ?? ''} onChange={v => onChange('tools.knowledgeServiceUrl', v)}
          placeholder="http://dks-server:8100" />
        <HelpText>Document Knowledge Server (DKS) の API エンドポイント。未設定時は knowledge ツール無効。</HelpText>
      </div>

      <div>
        <FieldLabel>Knowledge Namespaces</FieldLabel>
        <NamespaceEditor
          value={tools.knowledgeNamespaces ?? {}}
          onChange={v => onChange('tools.knowledgeNamespaces', v)}
          addDisabled
          addDisabledReason="新規 namespace 追加は MCP 経由を推奨"
          addDisabledHref="/help"
        />
        <HelpText>DKS の名前空間と API キーの組み合わせ。既存項目の編集・削除は可能ですが、新規追加は無効化されています。</HelpText>
      </div>
    </div>
  );
}
