import { useMemo, useState, type FormEvent } from 'react';
import { Pencil, Plus, RotateCcw, Search, SlidersHorizontal, Trash2 } from 'lucide-react';
import type {
  BaseModelCatalogItem,
  BaseModelUpsertRequest,
  CatalogProvider,
  PricingRuleSet,
  RuntimePolicySet,
} from '@easyai-ai-gateway/contracts';
import { Badge, Button, Card, CardContent, ConfirmDialog, FormDialog, FormItem, Label, Input, Select, Textarea } from '../../components/ui';
import type { LoadState } from '../../types';
import { BaseModelCapabilityEditor } from './BaseModelCapabilityEditor';
import {
  addCapabilityType,
  capabilitiesFromForm,
  capabilitiesToForm,
  createCapabilityEditorState,
  platformModelTypeDefinitions,
  removeCapabilityType,
  syncPrimaryCapability,
  type CapabilityEditorState,
} from './base-model-capabilities';
import { ModelCatalogCard } from './ModelCatalogCard';

type ModelForm = {
  providerKey: string;
  canonicalModelKey: string;
  invocationName: string;
  providerModelName: string;
  displayName: string;
  legacyAliasesText: string;
  officialModelId: string;
  officialDisplayName: string;
  lifecycleStage: string;
  replacementModel: string;
  retireAt: string;
  sourceUrl: string;
  verifiedAt: string;
  status: string;
  pricingRuleSetId: string;
  runtimePolicySetId: string;
  runtimePolicyOverrideJson: string;
  pricingVersion: string;
  capabilities: CapabilityEditorState;
  billingJson: string;
  rateLimitJson: string;
  metadataJson: string;
};

const statuses = ['active', 'deprecated', 'hidden'];
const fallbackTypes = platformModelTypeDefinitions.map((item) => item.key);

function createEmptyForm(modelType = 'text_generate'): ModelForm {
  return {
    providerKey: '',
    canonicalModelKey: '',
    invocationName: '',
    providerModelName: '',
    displayName: '',
    legacyAliasesText: '',
    officialModelId: '',
    officialDisplayName: '',
    lifecycleStage: 'stable',
    replacementModel: '',
    retireAt: '',
    sourceUrl: '',
    verifiedAt: '',
    status: 'active',
    pricingRuleSetId: '',
    runtimePolicySetId: '',
    runtimePolicyOverrideJson: '{}',
    pricingVersion: '1',
    capabilities: createCapabilityEditorState(modelType),
    billingJson: '{}',
    rateLimitJson: '{}',
    metadataJson: '{}',
  };
}

export function BaseModelCatalogPanel(props: {
  baseModels: BaseModelCatalogItem[];
  message: string;
  pricingRuleSets: PricingRuleSet[];
  providers: CatalogProvider[];
  runtimePolicySets: RuntimePolicySet[];
  state: LoadState;
  onDeleteBaseModel: (baseModelId: string) => Promise<void>;
  onResetAllBaseModels: () => Promise<void>;
  onResetBaseModel: (baseModelId: string) => Promise<void>;
  onSaveBaseModel: (input: BaseModelUpsertRequest, baseModelId?: string) => Promise<void>;
}) {
  const [dialogOpen, setDialogOpen] = useState(false);
  const [filterDialogOpen, setFilterDialogOpen] = useState(false);
  const [editingId, setEditingId] = useState('');
  const [form, setForm] = useState<ModelForm>(() => createEmptyForm());
  const [localError, setLocalError] = useState('');
  const [query, setQuery] = useState('');
  const [providerFilter, setProviderFilter] = useState('all');
  const [typeFilter, setTypeFilter] = useState('all');
  const [statusFilter, setStatusFilter] = useState('active');
  const [pendingDeleteModel, setPendingDeleteModel] = useState<BaseModelCatalogItem | null>(null);
  const [pendingResetModel, setPendingResetModel] = useState<BaseModelCatalogItem | null>(null);
  const [pendingResetAll, setPendingResetAll] = useState(false);

  const providerNames = useMemo(
    () => new Map(props.providers.map((item) => [item.providerKey, item.displayName])),
    [props.providers],
  );
  const typeOptions = useMemo(
    () => Array.from(new Set([...fallbackTypes, ...props.baseModels.flatMap(readModelTypes)])),
    [props.baseModels],
  );
  const providerOptions = useMemo(
    () => Array.from(new Set([...props.providers.map((item) => item.providerKey), ...props.baseModels.map((item) => item.providerKey)])),
    [props.baseModels, props.providers],
  );
  const defaultPricingRuleSetId = useMemo(() => defaultRuleSetId(props.pricingRuleSets), [props.pricingRuleSets]);
  const defaultRuntimePolicySetId = useMemo(() => defaultRuntimePolicyId(props.runtimePolicySets), [props.runtimePolicySets]);
  const systemModelCount = useMemo(() => props.baseModels.filter(isSystemBaseModel).length, [props.baseModels]);
  const customizedSystemModelCount = useMemo(
    () => props.baseModels.filter((item) => isSystemBaseModel(item) && item.customizedAt).length,
    [props.baseModels],
  );
  const filteredModels = useMemo(() => {
    const keyword = query.trim().toLowerCase();
    return props.baseModels.filter((item) => {
      const matchesProvider = providerFilter === 'all' || item.providerKey === providerFilter;
      const matchesType = typeFilter === 'all' || readModelTypes(item).includes(typeFilter);
      const matchesStatus = statusFilter === 'all' || item.status === statusFilter;
      const text = `${item.displayName} ${item.invocationName} ${item.providerModelName} ${item.canonicalModelKey} ${(item.legacyAliases ?? []).join(' ')}`.toLowerCase();
      return matchesProvider && matchesType && matchesStatus && (!keyword || text.includes(keyword));
    });
  }, [props.baseModels, providerFilter, query, statusFilter, typeFilter]);

  async function submit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setLocalError('');
    try {
      await props.onSaveBaseModel(formToPayload(form), editingId || undefined);
      closeDialog();
    } catch (err) {
      setLocalError(err instanceof Error ? err.message : '模型保存失败');
    }
  }

  function openCreateDialog() {
    const providerKey = providerOptions[0] ?? '';
    setEditingId('');
    setForm({
      ...createEmptyForm(),
      providerKey,
      canonicalModelKey: providerKey ? `${providerKey}:` : '',
      pricingRuleSetId: defaultPricingRuleSetId,
      runtimePolicySetId: defaultRuntimePolicySetId,
    });
    setDialogOpen(true);
  }

  function editModel(model: BaseModelCatalogItem) {
    setEditingId(model.id);
    setForm({
      ...modelToForm(model),
      pricingRuleSetId: model.pricingRuleSetId || defaultPricingRuleSetId,
      runtimePolicySetId: model.runtimePolicySetId || defaultRuntimePolicySetId,
    });
    setDialogOpen(true);
  }

  function closeDialog() {
    setEditingId('');
    setForm(createEmptyForm());
    setLocalError('');
    setDialogOpen(false);
  }

  async function deleteModel(model: BaseModelCatalogItem) {
    try {
      await props.onDeleteBaseModel(model.id);
      setPendingDeleteModel(null);
      if (editingId === model.id) closeDialog();
    } catch (err) {
      setLocalError(err instanceof Error ? err.message : '模型删除失败');
    }
  }

  async function resetModel(model: BaseModelCatalogItem) {
    try {
      await props.onResetBaseModel(model.id);
      setPendingResetModel(null);
      if (editingId === model.id) closeDialog();
    } catch (err) {
      setLocalError(err instanceof Error ? err.message : '模型重置失败');
    }
  }

  async function resetAllModels() {
    try {
      await props.onResetAllBaseModels();
      setPendingResetAll(false);
      if (editingId) closeDialog();
    } catch (err) {
      setLocalError(err instanceof Error ? err.message : '模型批量重置失败');
    }
  }

  function resetFilters() {
    setQuery('');
    setProviderFilter('all');
    setTypeFilter('all');
    setStatusFilter('active');
  }

  const advancedFilterCount = [
    providerFilter !== 'all',
    typeFilter !== 'all',
    statusFilter !== 'active',
  ].filter(Boolean).length;
  const filterActive = Boolean(query) || advancedFilterCount > 0;

  return (
    <div className="pageStack">
      <Card className="compactAdminTableCard">
        <CardContent className="compactAdminTableContent">
          <div className="compactAdminToolbar catalogCompactToolbar">
            <span className="platformSearchBox compactAdminSearch">
              <Search size={15} />
              <Input
                aria-label="搜索基准模型"
                value={query}
                placeholder="模型名称、调用名、Provider 模型名、Canonical Key 或别名"
                onChange={(event) => setQuery(event.target.value)}
              />
            </span>
            <div className="compactAdminToolbarActions">
              <Button type="button" variant="outline" size="sm" onClick={() => setFilterDialogOpen(true)}>
                <SlidersHorizontal size={14} />
                高级筛选{advancedFilterCount ? `（${advancedFilterCount}）` : ''}
              </Button>
              <Button type="button" variant="outline" size="sm" disabled={!filterActive} onClick={resetFilters}>
                <RotateCcw size={14} />
                重置
              </Button>
              <Button type="button" variant="outline" size="sm" disabled={!systemModelCount || props.state === 'loading'} onClick={() => setPendingResetAll(true)}>
                <RotateCcw size={14} />
                重置所有
              </Button>
              <Button type="button" size="sm" onClick={openCreateDialog}>
                <Plus size={15} />
                新增模型
              </Button>
            </div>
          </div>
          {(props.message || localError) && <p className="formMessage">{localError || props.message}</p>}
          <section className="baseModelGrid">
            {filteredModels.map((model) => (
              <ModelCard
                key={model.id}
                model={model}
                pricingRuleSetName={pricingRuleSetName(model.pricingRuleSetId, props.pricingRuleSets)}
                providerName={providerNames.get(model.providerKey)}
                runtimePolicySetName={runtimePolicySetName(model.runtimePolicySetId, props.runtimePolicySets)}
                onDelete={() => setPendingDeleteModel(model)}
                onEdit={() => editModel(model)}
                onReset={() => setPendingResetModel(model)}
              />
            ))}
            {!filteredModels.length && (
              <div className="emptyState">
                <strong>没有匹配的基准模型</strong>
              </div>
            )}
          </section>
        </CardContent>
      </Card>

      <FormDialog
        ariaLabel="基准模型高级筛选"
        className="compactFilterDialog"
        footer={(
          <>
            <Button type="submit">完成</Button>
            <Button type="button" variant="outline" onClick={() => {
              setProviderFilter('all');
              setTypeFilter('all');
              setStatusFilter('active');
            }}>
              重置高级条件
            </Button>
          </>
        )}
        open={filterDialogOpen}
        title="高级筛选"
        onClose={() => setFilterDialogOpen(false)}
        onSubmit={(event) => {
          event.preventDefault();
          setFilterDialogOpen(false);
        }}
      >
        <Label>Provider<Select value={providerFilter} onChange={(event) => setProviderFilter(event.target.value)}>
          <option value="all">全部 Provider</option>
          {providerOptions.map((item) => <option value={item} key={item}>{providerNames.get(item) ?? item}</option>)}
        </Select></Label>
        <Label>模型能力<Select value={typeFilter} onChange={(event) => setTypeFilter(event.target.value)}>
          <option value="all">全部能力</option>
          {typeOptions.map((item) => <option value={item} key={item}>{item}</option>)}
        </Select></Label>
        <Label>状态<Select value={statusFilter} onChange={(event) => setStatusFilter(event.target.value)}>
          <option value="active">仅 Active</option>
          <option value="deprecated">仅 Deprecated</option>
          <option value="hidden">仅 Hidden</option>
          <option value="all">全部状态</option>
        </Select></Label>
      </FormDialog>

      <FormDialog
        ariaLabel={editingId ? '编辑基准模型' : '新增基准模型'}
        className="baseModelDialog"
        eyebrow={editingId ? 'Edit Base Model' : 'New Base Model'}
        footer={(
          <>
            <Button type="submit" disabled={props.state === 'loading'}>
              {editingId ? <Pencil size={15} /> : <Plus size={15} />}
              {editingId ? '保存修改' : '新增模型'}
            </Button>
            <Button type="button" variant="outline" onClick={closeDialog}>
              <RotateCcw size={15} />
              取消
            </Button>
          </>
        )}
        formClassName="baseModelForm"
        open={dialogOpen}
        title={editingId ? '编辑基准模型' : '新增基准模型'}
        onClose={closeDialog}
        onSubmit={submit}
      >
        <FormItem label="Provider">
          <Select value={form.providerKey} onChange={(event) => setForm({ ...form, providerKey: event.target.value })}>
            <option value="">选择厂商</option>
            {providerOptions.map((item) => <option value={item} key={item}>{providerNames.get(item) ?? item}</option>)}
          </Select>
        </FormItem>
        <FormItem label="供应商真实名" description="实际发给上游平台的 providerModelName，可包含供应商前缀。">
          <Input value={form.providerModelName} onChange={(event) => setForm({ ...form, providerModelName: event.target.value })} />
        </FormItem>
        <FormItem description="API Token 用户推荐使用的官方模型 ID，保持官方大小写。" label="官方调用名">
          <Input value={form.invocationName} onChange={(event) => setForm({ ...form, invocationName: event.target.value })} />
        </FormItem>
        <FormItem description="只用于模型卡片标题，不参与路由。" label="显示名">
          <Input value={form.displayName} onChange={(event) => setForm({ ...form, displayName: event.target.value })} />
        </FormItem>
        <FormItem className="spanTwo" description="每行一个历史调用名；仅在 14 天兼容期内参与路由。" label="兼容别名">
          <Textarea value={form.legacyAliasesText} onChange={(event) => setForm({ ...form, legacyAliasesText: event.target.value })} rows={3} />
        </FormItem>
        <FormItem className="spanTwo" label="Canonical Key">
          <Input value={form.canonicalModelKey} onChange={(event) => setForm({ ...form, canonicalModelKey: event.target.value })} />
        </FormItem>
        <FormItem label="状态">
          <Select value={form.status} onChange={(event) => setForm({ ...form, status: event.target.value })}>
            {statuses.map((item) => <option value={item} key={item}>{item}</option>)}
          </Select>
        </FormItem>
        <FormItem label="生命周期">
          <Select value={form.lifecycleStage} onChange={(event) => setForm({ ...form, lifecycleStage: event.target.value })}>
            {['stable', 'preview', 'deprecated', 'provider', 'internal'].map((item) => <option value={item} key={item}>{item}</option>)}
          </Select>
        </FormItem>
        <FormItem label="替代模型">
          <Input value={form.replacementModel} onChange={(event) => setForm({ ...form, replacementModel: event.target.value })} />
        </FormItem>
        <FormItem label="官方模型 ID">
          <Input value={form.officialModelId} onChange={(event) => setForm({ ...form, officialModelId: event.target.value })} />
        </FormItem>
        <FormItem label="官方显示名">
          <Input value={form.officialDisplayName} onChange={(event) => setForm({ ...form, officialDisplayName: event.target.value })} />
        </FormItem>
        <FormItem label="退役时间">
          <Input value={form.retireAt} onChange={(event) => setForm({ ...form, retireAt: event.target.value })} placeholder="YYYY-MM-DD" />
        </FormItem>
        <FormItem label="核验时间">
          <Input value={form.verifiedAt} onChange={(event) => setForm({ ...form, verifiedAt: event.target.value })} placeholder="YYYY-MM-DD" />
        </FormItem>
        <FormItem className="spanTwo" label="官方依据链接">
          <Input value={form.sourceUrl} onChange={(event) => setForm({ ...form, sourceUrl: event.target.value })} />
        </FormItem>
        <FormItem label="计价规则">
          <Select value={form.pricingRuleSetId || defaultPricingRuleSetId} onChange={(event) => setForm({ ...form, pricingRuleSetId: event.target.value })}>
            {!props.pricingRuleSets.length && <option value="">暂无可用计价规则</option>}
            {props.pricingRuleSets.map((item) => <option value={item.id} key={item.id}>{item.name}</option>)}
          </Select>
        </FormItem>
        <FormItem label="运行策略">
          <Select value={form.runtimePolicySetId || defaultRuntimePolicySetId} onChange={(event) => setForm({ ...form, runtimePolicySetId: event.target.value })}>
            {!props.runtimePolicySets.length && <option value="">暂无可用运行策略</option>}
            {props.runtimePolicySets.map((item) => <option value={item.id} key={item.id}>{item.name}</option>)}
          </Select>
        </FormItem>
        <FormItem label="定价版本">
          <Input value={form.pricingVersion} onChange={(event) => setForm({ ...form, pricingVersion: event.target.value })} />
        </FormItem>
        <BaseModelCapabilityEditor
          modelType={primaryCapabilityType(form.capabilities)}
          typeOptions={typeOptions}
          value={form.capabilities}
          onChange={(capabilities) => setForm({ ...form, capabilities })}
        />
        <JsonField label="基准计费 JSON" value={form.billingJson} onChange={(value) => setForm({ ...form, billingJson: value })} />
        <JsonField label="旧版限流 JSON（兼容字段）" value={form.rateLimitJson} onChange={(value) => setForm({ ...form, rateLimitJson: value })} />
        <JsonField label="运行策略覆盖 JSON" value={form.runtimePolicyOverrideJson} onChange={(value) => setForm({ ...form, runtimePolicyOverrideJson: value })} />
        <JsonField label="元数据 JSON" value={form.metadataJson} onChange={(value) => setForm({ ...form, metadataJson: value })} />
      </FormDialog>
      <ConfirmDialog
        confirmLabel="删除模型"
        description="删除后该基准模型不可恢复，已关联的平台模型可能无法继续跟随基准配置。"
        loading={props.state === 'loading'}
        open={Boolean(pendingDeleteModel)}
        title={`确认删除基准模型 ${baseModelAlias(pendingDeleteModel)}？`}
        onCancel={() => setPendingDeleteModel(null)}
        onConfirm={() => pendingDeleteModel ? deleteModel(pendingDeleteModel) : undefined}
      />
      <ConfirmDialog
        confirmLabel="重置为默认"
        description="系统内置模型会恢复到默认模型库快照，当前手动修改的能力、计价、运行策略和元数据会被默认值覆盖。"
        loading={props.state === 'loading'}
        open={Boolean(pendingResetModel)}
        title={`确认重置 ${baseModelAlias(pendingResetModel)}？`}
        onCancel={() => setPendingResetModel(null)}
        onConfirm={() => pendingResetModel ? resetModel(pendingResetModel) : undefined}
      />
      <ConfirmDialog
        confirmLabel="重置所有"
        description={`将恢复 ${systemModelCount} 个系统内置基准模型的默认快照，其中 ${customizedSystemModelCount} 个显示为已修改。用户添加的模型不会被影响。`}
        loading={props.state === 'loading'}
        open={pendingResetAll}
        title="确认重置所有系统内置模型？"
        onCancel={() => setPendingResetAll(false)}
        onConfirm={resetAllModels}
      />
    </div>
  );
}

function ModelCard(props: {
  model: BaseModelCatalogItem;
  pricingRuleSetName: string;
  providerName?: string;
  runtimePolicySetName: string;
  onDelete: () => void;
  onEdit: () => void;
  onReset: () => void;
}) {
  const types = readModelTypes(props.model);
  const chips = [...types.slice(0, 5), ...(types.length > 5 ? [`+${types.length - 5}`] : [])];
  const isSystem = isSystemBaseModel(props.model);
  return (
    <ModelCatalogCard
      actions={(
        <>
          {isSystem && (
            <Button type="button" variant="outline" size="sm" onClick={props.onReset}>
              <RotateCcw size={14} />
              重置
            </Button>
          )}
          <Button type="button" variant="outline" size="sm" onClick={props.onEdit}>
            <Pencil size={14} />
            修改
          </Button>
          <Button type="button" variant="destructive" size="sm" disabled={props.model.referenceCount > 0} onClick={props.onDelete}>
            <Trash2 size={14} />
            删除
          </Button>
        </>
      )}
      badges={[
        <Badge key="status" variant={props.model.status === 'active' ? 'success' : 'secondary'}>{props.model.status}</Badge>,
        <Badge key="catalog-type" variant={isSystem ? 'secondary' : 'outline'}>{baseModelCatalogTypeLabel(props.model)}</Badge>,
        ...(isSystem && props.model.customizedAt ? [<Badge key="customized" variant="warning">已修改</Badge>] : []),
        <Badge key="references" variant={props.model.referenceCount ? 'warning' : 'outline'}>{props.model.referenceCount} 个引用</Badge>,
      ]}
      chips={chips}
      iconPath={readModelIconPath(props.model)}
      iconText={props.model.providerKey}
      meta={[props.providerName ?? props.model.providerKey, props.pricingRuleSetName, props.runtimePolicySetName, props.model.canonicalModelKey]}
      subtitle={`${props.model.invocationName} · 上游 ${props.model.providerModelName}`}
      title={props.model.displayName || props.model.invocationName}
    />
  );
}

function JsonField(props: { label: string; value: string; onChange: (value: string) => void }) {
  return (
    <Label className="spanTwo">
      {props.label}
      <Textarea value={props.value} onChange={(event) => props.onChange(event.target.value)} rows={5} spellCheck={false} />
    </Label>
  );
}

function modelToForm(model: BaseModelCatalogItem): ModelForm {
  const capabilities = readObject(model.capabilities);
  const metadataTypes = Array.isArray(model.metadata?.originalTypes) ? { originalTypes: model.metadata.originalTypes } : {};
  const modelTypes = readModelTypes(model);
  const capabilityState = capabilitiesToForm({ ...capabilities, ...metadataTypes }, modelTypes[0] ?? 'text_generate');
  return {
    providerKey: model.providerKey,
    canonicalModelKey: model.canonicalModelKey,
    invocationName: model.invocationName || model.modelAlias || model.providerModelName,
    providerModelName: model.providerModelName,
    displayName: model.displayName || model.invocationName || model.providerModelName,
    legacyAliasesText: (model.legacyAliases ?? []).join('\n'),
    officialModelId: readString(model.metadata?.officialModelId),
    officialDisplayName: readString(model.metadata?.officialDisplayName),
    lifecycleStage: readString(model.metadata?.lifecycleStage) || 'stable',
    replacementModel: readString(model.metadata?.replacementModel),
    retireAt: readString(model.metadata?.retireAt),
    sourceUrl: readString(model.metadata?.sourceUrl),
    verifiedAt: readString(model.metadata?.verifiedAt),
    status: model.status,
    pricingRuleSetId: model.pricingRuleSetId ?? '',
    runtimePolicySetId: model.runtimePolicySetId ?? '',
    pricingVersion: String(model.pricingVersion || 1),
    capabilities: capabilityState,
    billingJson: stringifyJson(model.baseBillingConfig),
    rateLimitJson: stringifyJson(model.defaultRateLimitPolicy),
    runtimePolicyOverrideJson: stringifyJson(model.runtimePolicyOverride),
    metadataJson: stringifyJson(model.metadata),
  };
}

function readModelIconPath(model: BaseModelCatalogItem) {
  return readString(model.metadata?.iconPath) || readString(readRecord(model.metadata?.rawModel)?.icon_path);
}

function baseModelAlias(model?: BaseModelCatalogItem | null) {
  return model?.invocationName || model?.modelAlias || model?.providerModelName || '';
}

function primaryCapabilityType(capabilities: CapabilityEditorState) {
  return capabilities.types[0] ?? 'text_generate';
}

function isSystemBaseModel(model: BaseModelCatalogItem) {
  if (model.catalogType) return model.catalogType === 'system';
  return model.metadata?.source === 'server-main.integration-platform' || Boolean(model.metadata?.rawModel || model.metadata?.sourceProviderCode);
}

function baseModelCatalogTypeLabel(model: BaseModelCatalogItem) {
  return isSystemBaseModel(model) ? '系统内置' : '用户添加';
}

function formToPayload(form: ModelForm): BaseModelUpsertRequest {
  const modelTypes = uniqueStrings(form.capabilities.types).filter(Boolean);
  const primaryType = modelTypes[0] ?? 'text_generate';
  const capabilities = syncCapabilityTypes(form.capabilities, modelTypes);
  const invocationName = form.invocationName.trim() || form.providerModelName.trim();
  const displayName = form.displayName.trim() || invocationName;
  const metadata = parseJsonObject(form.metadataJson, '元数据 JSON');
  return {
    providerKey: form.providerKey.trim(),
    canonicalModelKey: form.canonicalModelKey.trim() || undefined,
    invocationName,
    providerModelName: form.providerModelName.trim(),
    modelType: modelTypes.length ? modelTypes : [primaryType],
    modelAlias: invocationName,
    displayName,
    legacyAliases: uniqueStrings(form.legacyAliasesText.split(/[\n,]/)),
    capabilities: capabilitiesFromForm(syncPrimaryCapability(capabilities, primaryType)),
    baseBillingConfig: parseJsonObject(form.billingJson, '基准计费 JSON'),
    defaultRateLimitPolicy: parseJsonObject(form.rateLimitJson, '限流 JSON'),
    pricingRuleSetId: form.pricingRuleSetId.trim() || undefined,
    runtimePolicySetId: form.runtimePolicySetId.trim() || undefined,
    runtimePolicyOverride: parseJsonObject(form.runtimePolicyOverrideJson, '运行策略覆盖 JSON'),
    metadata: {
      ...metadata,
      alias: invocationName,
      officialModelId: form.officialModelId.trim() || invocationName,
      officialDisplayName: form.officialDisplayName.trim() || displayName,
      lifecycleStage: form.lifecycleStage,
      replacementModel: form.replacementModel.trim() || undefined,
      retireAt: form.retireAt.trim() || undefined,
      sourceUrl: form.sourceUrl.trim() || undefined,
      verifiedAt: form.verifiedAt.trim() || undefined,
      originalTypes: modelTypes,
    },
    pricingVersion: Number.parseInt(form.pricingVersion, 10) || 1,
    status: form.status,
  };
}

function uniqueStrings(values: string[]) {
  return Array.from(new Set(values.map((item) => item.trim()).filter(Boolean)));
}

function syncCapabilityTypes(state: CapabilityEditorState, modelTypes: string[]): CapabilityEditorState {
  const nextTypes = uniqueStrings(modelTypes).filter(Boolean);
  if (!nextTypes.length) return state;
  let next = state;
  nextTypes.forEach((type) => {
    next = addCapabilityType(next, type);
  });
  next.types
    .filter((type) => !nextTypes.includes(type))
    .forEach((type) => {
      next = removeCapabilityType(next, type);
    });
  return { ...next, types: nextTypes };
}

function readModelTypes(model: BaseModelCatalogItem) {
  if (Array.isArray(model.modelType)) return model.modelType.map(String);
  const values = model.metadata?.originalTypes ?? model.capabilities?.originalTypes;
  if (Array.isArray(values)) return values.map(String);
  return [];
}

function defaultRuleSetId(ruleSets: PricingRuleSet[]) {
  return ruleSets.find((item) => item.ruleSetKey === 'default-multimodal-v1')?.id ?? ruleSets[0]?.id ?? '';
}

function defaultRuntimePolicyId(policySets: RuntimePolicySet[]) {
  return policySets.find((item) => item.policyKey === 'default-runtime-v1')?.id ?? policySets[0]?.id ?? '';
}

function pricingRuleSetName(ruleSetId: string | undefined, ruleSets: PricingRuleSet[]) {
  if (!ruleSetId) return defaultRuleSetId(ruleSets) ? '默认计价规则' : '未绑定计价规则';
  return ruleSets.find((item) => item.id === ruleSetId)?.name ?? '未知计价规则';
}

function runtimePolicySetName(policySetId: string | undefined, policySets: RuntimePolicySet[]) {
  if (!policySetId) return defaultRuntimePolicyId(policySets) ? '默认运行策略' : '未绑定运行策略';
  return policySets.find((item) => item.id === policySetId)?.name ?? '未知运行策略';
}

function parseJsonObject(value: string, label: string) {
  try {
    const parsed = value.trim() ? JSON.parse(value) : {};
    if (!parsed || Array.isArray(parsed) || typeof parsed !== 'object') {
      throw new Error(`${label} 必须是 JSON object`);
    }
    return parsed as Record<string, unknown>;
  } catch (err) {
    if (err instanceof Error && err.message.includes('必须是')) throw err;
    throw new Error(`${label} 格式不正确`);
  }
}

function stringifyJson(value?: object) {
  return JSON.stringify(value ?? {}, null, 2);
}

function readObject(value: unknown) {
  return value && !Array.isArray(value) && typeof value === 'object' ? value as Record<string, unknown> : {};
}

function readString(value: unknown) {
  return typeof value === 'string' ? value : '';
}

function readRecord(value: unknown) {
  return value && !Array.isArray(value) && typeof value === 'object' ? (value as Record<string, unknown>) : undefined;
}
