import { useEffect, useMemo, useState, type FormEvent } from 'react';
import { Popover as AntPopover } from 'antd';
import { CheckCircle2, History, RotateCcw, Search, SlidersHorizontal } from 'lucide-react';
import type { IntegrationPlatform, ModelRateLimitStatus, PlatformDynamicPriorityUpdateRequest, PlatformPolicyEvent, PriorityDemotionRecord, WorkerClusterRuntime, WorkerInstanceRuntime } from '@easyai-ai-gateway/contracts';
import { Badge, Button, Card, CardContent, EmptyState, FormDialog, Input, Label, Select, Table, TableCell, TableHead, TableRow } from '../../components/ui';

export function RealtimeLoadPanel(props: {
  modelRateLimits: ModelRateLimitStatus[];
  modelRateLimitsUpdatedAt: number | null;
  platforms: IntegrationPlatform[];
  workerClusterRuntime: WorkerClusterRuntime | null;
  onSavePlatformDynamicPriority: (platformId: string, input: PlatformDynamicPriorityUpdateRequest) => Promise<void>;
  onRestoreRuntimeModel: (platformModelId: string) => Promise<void>;
}) {
  const [now, setNow] = useState(() => Date.now());
  const [priorityDialog, setPriorityDialog] = useState<PriorityDialogState | null>(null);
  const [priorityError, setPriorityError] = useState('');
  const [prioritySaving, setPrioritySaving] = useState(false);
  const [restoreSavingId, setRestoreSavingId] = useState<string | null>(null);
  const [query, setQuery] = useState('');
  const [platformId, setPlatformId] = useState('');
  const [availability, setAvailability] = useState<RealtimeLoadAvailability>('enabled');
  const [modelType, setModelType] = useState('');
  const [runtimeState, setRuntimeState] = useState('');
  const [sortField, setSortField] = useState<RealtimeLoadSortField>('load');
  const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('desc');
  const [filterDialogOpen, setFilterDialogOpen] = useState(false);
  const platformMap = useMemo(() => new Map(props.platforms.map((item) => [item.id, item])), [props.platforms]);
  const modelTypeOptions = useMemo(
    () => Array.from(new Set(props.modelRateLimits.flatMap((status) => status.modelType))).sort((a, b) => a.localeCompare(b)),
    [props.modelRateLimits],
  );
  const filteredStatuses = useMemo(
    () => filterAndSortRealtimeLoad(props.modelRateLimits, platformMap, {
      query,
      platformId,
      availability,
      modelType,
      runtimeState,
      sortField,
      sortDirection,
      now,
    }),
    [availability, modelType, now, platformId, platformMap, props.modelRateLimits, query, runtimeState, sortDirection, sortField],
  );
  const hasActiveFilters = Boolean(query || platformId || modelType || runtimeState || availability !== 'all');
  const advancedFilterCount = [
    modelType,
    runtimeState,
    sortDirection === 'desc' ? '' : sortDirection,
  ].filter(Boolean).length;
  const hasActiveSettings = Boolean(query || platformId || availability !== 'enabled' || advancedFilterCount || sortField !== 'load');

  useEffect(() => {
    const timer = window.setInterval(() => setNow(Date.now()), 1000);
    return () => window.clearInterval(timer);
  }, []);

  function openPriorityDialog(status: ModelRateLimitStatus, platform: IntegrationPlatform | undefined) {
    setPriorityError('');
    setPriorityDialog({
      platform,
      status,
      value: formatPriority(platformEffectivePriority(status, platform)),
    });
  }

  function closePriorityDialog() {
    if (prioritySaving) return;
    setPriorityDialog(null);
    setPriorityError('');
  }

  async function submitPriorityForm(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    if (!priorityDialog) return;
    const dynamicPriority = parsePriorityInput(priorityDialog.value);
    if (dynamicPriority === null) {
      setPriorityError('请输入大于等于 0 的整数。');
      return;
    }
    await savePriority(priorityDialog.status.platformId, { dynamicPriority });
  }

  async function resetPriority() {
    if (!priorityDialog) return;
    await savePriority(priorityDialog.status.platformId, { reset: true });
  }

  async function savePriority(platformId: string, input: PlatformDynamicPriorityUpdateRequest) {
    setPrioritySaving(true);
    setPriorityError('');
    try {
      await props.onSavePlatformDynamicPriority(platformId, input);
      setPriorityDialog(null);
    } catch (err) {
      setPriorityError(err instanceof Error ? err.message : '更新平台动态优先级失败');
    } finally {
      setPrioritySaving(false);
    }
  }

  async function restoreRuntimeModel(platformModelId: string) {
    setRestoreSavingId(platformModelId);
    try {
      await props.onRestoreRuntimeModel(platformModelId);
    } catch {
      // App-level state owns the error message.
    } finally {
      setRestoreSavingId(null);
    }
  }

  function resetFilters() {
    setQuery('');
    setPlatformId('');
    setAvailability('enabled');
    setModelType('');
    setRuntimeState('');
    setSortField('load');
    setSortDirection('desc');
  }

  return (
    <section className="pageStack">
      <WorkerRuntimeTable runtime={props.workerClusterRuntime} />
      <Card className="compactAdminTableCard">
        <CardContent className="compactAdminTableContent">
          <div className="compactAdminToolbar realtimeCompactToolbar">
            <span className="platformSearchBox compactAdminSearch">
              <Search size={15} />
              <Input
                aria-label="搜索实时负载"
                value={query}
                placeholder="平台、模型、Provider 或模型类型"
                onChange={(event) => setQuery(event.target.value)}
              />
            </span>
            <Select
              aria-label="平台筛选"
              value={platformId || 'all'}
              onChange={(event) => setPlatformId(event.target.value === 'all' ? '' : event.target.value)}
            >
              <option value="all">全部平台</option>
              {props.platforms.map((platform) => <option value={platform.id} key={platform.id}>{platformDisplayName(platform)}</option>)}
            </Select>
            <Select
              aria-label="实时负载排序"
              value={sortField}
              onChange={(event) => setSortField(event.target.value as RealtimeLoadSortField)}
            >
              <option value="load">满载率</option>
              <option value="queued">排队数</option>
              <option value="concurrent">并发</option>
              <option value="tpm">TPM</option>
              <option value="rpm">RPM</option>
              <option value="priority">平台优先级</option>
            </Select>
            <Select
              aria-label="启用状态筛选"
              value={availability}
              onChange={(event) => setAvailability(event.target.value as RealtimeLoadAvailability)}
            >
              <option value="enabled">仅看启用</option>
              <option value="all">全部状态</option>
              <option value="disabled">仅看禁用</option>
            </Select>
            <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={!hasActiveSettings} onClick={resetFilters}>
                <RotateCcw size={14} />
                重置
              </Button>
            </div>
          </div>
          <RateLimitStatusTable
            filtered={hasActiveFilters}
            now={now}
            platformMap={platformMap}
            statuses={filteredStatuses}
            onAdjustPriority={openPriorityDialog}
            onRestoreRuntimeModel={restoreRuntimeModel}
            restoreSavingId={restoreSavingId}
          />
        </CardContent>
      </Card>
      <FormDialog
        ariaLabel="实时负载高级筛选"
        className="compactFilterDialog"
        closeLabel="关闭"
        footer={(
          <>
            <Button
              type="button"
              variant="outline"
              disabled={!advancedFilterCount}
              onClick={() => {
                setModelType('');
                setRuntimeState('');
                setSortDirection('desc');
              }}
            >
              <RotateCcw size={15} />
              清空高级条件
            </Button>
            <Button type="submit">完成</Button>
          </>
        )}
        open={filterDialogOpen}
        title="实时负载高级筛选"
        onClose={() => setFilterDialogOpen(false)}
        onSubmit={(event) => {
          event.preventDefault();
          setFilterDialogOpen(false);
        }}
      >
        <Label>
          模型类型
          <Select value={modelType || 'all'} onChange={(event) => setModelType(event.target.value === 'all' ? '' : event.target.value)}>
            <option value="all">全部类型</option>
            {modelTypeOptions.map((type) => <option value={type} key={type}>{type}</option>)}
          </Select>
        </Label>
        <Label>
          运行状态
          <Select value={runtimeState || 'all'} onChange={(event) => setRuntimeState(event.target.value === 'all' ? '' : event.target.value)}>
            <option value="all">全部状态</option>
            <option value="normal">正常</option>
            <option value="high">高负载 ≥ 80%</option>
            <option value="queued">有排队</option>
            <option value="attention">冷却 / 停用</option>
          </Select>
        </Label>
        <Label>
          排序方向
          <Select value={sortDirection} onChange={(event) => setSortDirection(event.target.value as 'asc' | 'desc')}>
            <option value="desc">降序</option>
            <option value="asc">升序</option>
          </Select>
        </Label>
      </FormDialog>
      <PlatformPriorityDialog
        dialog={priorityDialog}
        error={priorityError}
        saving={prioritySaving}
        onClose={closePriorityDialog}
        onReset={resetPriority}
        onSubmit={submitPriorityForm}
        onValueChange={(value) => setPriorityDialog((current) => current ? { ...current, value } : current)}
      />
    </section>
  );
}

function WorkerRuntimeTable(props: { runtime: WorkerClusterRuntime | null }) {
  const workers = props.runtime?.workers ?? [];
  const queue = props.runtime?.queue;
  return (
    <Card className="compactAdminTableCard">
      <CardContent className="compactAdminTableContent">
        <div className="compactAdminToolbar">
          <span className="platformTableName">
            <strong>Worker 自适应负载</strong>
            <small>
              {queue
                ? `共享队列 ${queue.queued}，运行 ${queue.running}，最老等待 ${Math.round(queue.oldestWaitSeconds)} 秒`
                : '等待集群负载快照'}
            </small>
          </span>
        </div>
        {!workers.length ? (
          <EmptyState title="暂无活跃 Worker" description="Worker 心跳后会显示安全容量、阶段分布和压力状态。" />
        ) : (
          <div className="platformLimitTableViewport">
            <Table className="platformDataTable platformLimitTable" density="compact">
              <TableRow className="shTableHeader">
                <TableHead>Worker</TableHead>
                <TableHead>压力</TableHead>
                <TableHead className="platformLimitNumberHead">领取 / 安全 / Hard</TableHead>
                <TableHead className="platformLimitNumberHead">重负载许可</TableHead>
                <TableHead className="platformLimitNumberHead">准备 / 等待 / 收尾</TableHead>
                <TableHead className="platformLimitNumberHead">任务 / 厂商租约</TableHead>
                <TableHead>最近采样</TableHead>
              </TableRow>
              {workers.map((worker) => (
                <TableRow key={worker.instanceId}>
                  <TableCell>
                    <span className="platformTableName">
                      <strong>{worker.podName || worker.instanceId}</strong>
                      <small>{[worker.site, shortId(worker.revision)].filter(Boolean).join(' · ') || shortId(worker.instanceId)}</small>
                    </span>
                  </TableCell>
                  <TableCell>{workerPressureCell(worker)}</TableCell>
                  <TableCell className="platformLimitNumberCell">
                    <strong>{worker.allocatedCapacity} / {worker.safeCapacity} / {worker.hardCapacityLimit}</strong>
                  </TableCell>
                  <TableCell className="platformLimitNumberCell">{worker.heavyCapacity}</TableCell>
                  <TableCell className="platformLimitNumberCell">
                    <span className="rateMetricCell">
                      <strong>{worker.preparingTasks} / {worker.waitingUpstreamTasks} / {worker.finalizingTasks}</strong>
                      <small>上报活跃 {worker.reportedActiveTasks}</small>
                    </span>
                  </TableCell>
                  <TableCell className="platformLimitNumberCell">{worker.runningTasks} / {worker.activeLeases}</TableCell>
                  <TableCell>
                    <span className="platformTableName">
                      <strong>{formatDateTime(worker.loadSampledAt) || '-'}</strong>
                      <small>心跳 {formatDateTime(worker.heartbeatAt)}</small>
                    </span>
                  </TableCell>
                </TableRow>
              ))}
            </Table>
          </div>
        )}
      </CardContent>
    </Card>
  );
}

function workerPressureCell(worker: WorkerInstanceRuntime) {
  const variant = worker.pressureState === 'critical'
    ? 'destructive'
    : worker.pressureState === 'busy'
      ? 'warning'
      : worker.pressureState === 'normal'
        ? 'success'
        : 'secondary';
  const label = worker.pressureState === 'critical'
    ? '临界'
    : worker.pressureState === 'busy'
      ? '繁忙'
      : worker.pressureState === 'normal'
        ? '正常'
        : '未知';
  return (
    <span className="platformTableName">
      <strong><Badge variant={variant}>{label}</Badge></strong>
      <small>{worker.pressureReason || '无压力原因'}</small>
    </span>
  );
}

type PriorityDialogState = {
  platform: IntegrationPlatform | undefined;
  status: ModelRateLimitStatus;
  value: string;
};

type RealtimeLoadSortField = 'load' | 'queued' | 'concurrent' | 'tpm' | 'rpm' | 'priority';

function RateLimitStatusTable(props: {
  filtered: boolean;
  statuses: ModelRateLimitStatus[];
  platformMap: Map<string, IntegrationPlatform>;
  now: number;
  onAdjustPriority: (status: ModelRateLimitStatus, platform: IntegrationPlatform | undefined) => void;
  onRestoreRuntimeModel: (platformModelId: string) => Promise<void>;
  restoreSavingId: string | null;
}) {
  if (!props.statuses.length) {
    if (props.filtered) {
      return <EmptyState title="没有匹配的实时负载" description="调整平台、模型、运行状态或关键词后再试。" />;
    }
    return <EmptyState title="暂无实时负载" description="模型产生请求后会在这里显示实时 RPM、TPM 和并发窗口。" />;
  }
  return (
    <section className="platformLimitView">
      <div className="platformLimitTableViewport">
        <Table className="platformDataTable platformLimitTable" density="compact">
          <TableRow className="shTableHeader">
            <TableHead>模型</TableHead>
            <TableHead>平台</TableHead>
            <TableHead className="platformLimitStatusHead">状态</TableHead>
            <TableHead className="platformLimitNumberHead">平台优先级</TableHead>
            <TableHead className="platformLimitNumberHead">满载率</TableHead>
            <TableHead className="platformLimitMetricHead platformLimitNumberHead" title="正在执行 / 并发上限 / 排队任务">
              <span>并发</span>
              <small>正在执行 / 并发 / 排队</small>
            </TableHead>
            <TableHead className="platformLimitNumberHead">TPM</TableHead>
            <TableHead className="platformLimitNumberHead">RPM</TableHead>
          </TableRow>
          {props.statuses.map((status) => {
            const platform = props.platformMap.get(status.platformId);
            return (
              <TableRow key={status.platformModelId}>
                <TableCell>
                  <span className="platformTableName">
                    <strong>{status.displayName || status.modelAlias || status.modelName}</strong>
                    <small>{status.providerModelName || status.modelName}</small>
                  </span>
                </TableCell>
                <TableCell>
                  <span className="platformTableName">
                    <strong>{platform ? platformDisplayName(platform) : status.platformName}</strong>
                    <small>{status.provider}</small>
                  </span>
                </TableCell>
                <TableCell className="platformLimitStatusCell">
                  {modelRuntimeStatusCell(
                    status,
                    platform,
                    props.now,
                    props.onRestoreRuntimeModel,
                    props.restoreSavingId === status.platformModelId,
                  )}
                </TableCell>
                <TableCell className="platformLimitNumberCell">{platformPriorityCell(status, platform, props.onAdjustPriority)}</TableCell>
                <TableCell className="platformLimitNumberCell">
                  <span className="rateLoadCell" data-overloaded={status.loadRatio > 0.8 ? 'true' : undefined}>
                    <strong>{formatPercent(status.loadRatio)}</strong>
                    <span className="rateLoadTrack"><i style={{ width: `${Math.min(status.loadRatio * 100, 100)}%` }} /></span>
                  </span>
                </TableCell>
                <TableCell className="platformLimitNumberCell">{concurrencyMetricCell(status)}</TableCell>
                <TableCell className="platformLimitNumberCell">{metricCell(status.tpm, true)}</TableCell>
                <TableCell className="platformLimitNumberCell">{metricCell(status.rpm)}</TableCell>
              </TableRow>
            );
          })}
        </Table>
      </div>
    </section>
  );
}

export function filterAndSortRealtimeLoad(
  statuses: ModelRateLimitStatus[],
  platformMap: Map<string, IntegrationPlatform>,
  options: {
    query: string;
    platformId: string;
    availability: RealtimeLoadAvailability;
    modelType: string;
    runtimeState: string;
    sortField: RealtimeLoadSortField;
    sortDirection: 'asc' | 'desc';
    now: number;
  },
) {
  const keyword = options.query.trim().toLowerCase();
  const filtered = statuses.filter((status) => {
    const platform = platformMap.get(status.platformId);
    const text = [
      status.displayName,
      status.modelName,
      status.providerModelName,
      status.modelAlias,
      status.provider,
      status.platformName,
      platform?.name,
      platform?.internalName,
      platform?.platformKey,
      ...status.modelType,
    ].filter(Boolean).join(' ').toLowerCase();
    const cooling = cooldownRemainingMs(status.modelCooldownUntil, options.now) > 0 ||
      cooldownRemainingMs(status.platformCooldownUntil, options.now) > 0;
    const disabled = !status.enabled || status.platformStatus !== 'enabled';
    const matchesAvailability = options.availability === 'all' ||
      (options.availability === 'disabled' ? disabled : !disabled);
    const queuedTasks = admissionQueueDepth(status);
    const matchesRuntime = !options.runtimeState ||
      (options.runtimeState === 'high' && status.loadRatio >= 0.8) ||
      (options.runtimeState === 'queued' && queuedTasks > 0) ||
      (options.runtimeState === 'attention' && (cooling || disabled)) ||
      (options.runtimeState === 'normal' && !cooling && !disabled && status.loadRatio < 0.8 && queuedTasks <= 0);
    return (!keyword || text.includes(keyword)) &&
      (!options.platformId || status.platformId === options.platformId) &&
      (!options.modelType || status.modelType.includes(options.modelType)) &&
      matchesAvailability &&
      matchesRuntime;
  });
  const direction = options.sortDirection === 'asc' ? 1 : -1;
  return filtered.sort((left, right) => {
    const difference = realtimeLoadSortValue(left, options.sortField) - realtimeLoadSortValue(right, options.sortField);
    if (difference !== 0) return difference * direction;
    return (left.displayName || left.modelName).localeCompare(right.displayName || right.modelName);
  });
}

export type RealtimeLoadAvailability = 'enabled' | 'disabled' | 'all';

function realtimeLoadSortValue(status: ModelRateLimitStatus, field: RealtimeLoadSortField) {
  if (field === 'queued') return admissionQueueDepth(status);
  if (field === 'concurrent') return status.concurrent.currentValue;
  if (field === 'tpm') return status.tpm.currentValue;
  if (field === 'rpm') return status.rpm.currentValue;
  if (field === 'priority') return status.platformEffectivePriority;
  return status.loadRatio;
}

function admissionQueueDepth(status: ModelRateLimitStatus) {
  if (status.waitingSyncTasks !== undefined || status.waitingAsyncTasks !== undefined) {
    return (status.waitingSyncTasks ?? 0) + (status.waitingAsyncTasks ?? 0);
  }
  return status.queuedTasks ?? 0;
}

function platformDisplayName(platform: IntegrationPlatform) {
  return platform.internalName?.trim() || platform.name;
}

function platformPriorityCell(
  status: ModelRateLimitStatus,
  platform: IntegrationPlatform | undefined,
  onAdjustPriority: (status: ModelRateLimitStatus, platform: IntegrationPlatform | undefined) => void,
) {
  const records = status.recentPriorityDemotions ?? [];
  const content = <PriorityDemotionPopover records={records} />;
  return (
    <span className="platformPriorityCell">
      <AntPopover
        align={{ offset: [0, 8] }}
        content={content}
        overlayClassName="priorityDemotionAntPopover"
        placement="bottomLeft"
        trigger={['hover', 'focus']}
      >
        <button className="priorityDemotionTrigger" type="button" aria-label={priorityDemotionAriaLabel(status, platform)}>
          <span className="rateMetricCell">
            <strong>{formatPriority(platformEffectivePriority(status, platform))}</strong>
            <small>{platformPrioritySubtitle(status, platform, records.length)}</small>
          </span>
        </button>
      </AntPopover>
      <Button className="priorityAdjustButton" type="button" variant="outline" size="xs" onClick={() => onAdjustPriority(status, platform)}>
        <SlidersHorizontal size={13} />
        调整
      </Button>
    </span>
  );
}

function PlatformPriorityDialog(props: {
  dialog: PriorityDialogState | null;
  error: string;
  saving: boolean;
  onClose: () => void;
  onReset: () => void;
  onSubmit: (event: FormEvent<HTMLFormElement>) => void;
  onValueChange: (value: string) => void;
}) {
  const dialog = props.dialog;
  const platform = dialog?.platform;
  const status = dialog?.status;
  return (
    <FormDialog
      bodyClassName="platformPriorityDialogBody"
      className="platformPriorityDialog"
      closeLabel="关闭"
      eyebrow="Runtime Priority"
      footer={(
        <>
          <Button type="button" variant="outline" disabled={props.saving} onClick={props.onClose}>
            取消
          </Button>
          <Button type="button" variant="secondary" disabled={props.saving || !dialog} onClick={props.onReset}>
            <RotateCcw size={15} />
            重置为静态优先级
          </Button>
          <Button type="submit" disabled={props.saving || !dialog}>
            <CheckCircle2 size={15} />
            保存动态优先级
          </Button>
        </>
      )}
      open={Boolean(dialog)}
      title="调整平台动态优先级"
      onClose={props.onClose}
      onSubmit={props.onSubmit}
    >
      <div className="platformPriorityDialogSummary">
        <span>
          <strong>{platform ? platformDisplayName(platform) : status?.platformName}</strong>
          <small>{status?.provider}</small>
        </span>
        <span>
          <strong>{formatPriority(platformEffectivePriority(status, platform))}</strong>
          <small>当前生效</small>
        </span>
      </div>
      <div className="platformPriorityDialogMetrics">
        <span>静态 {formatPriority(platformStaticPriority(status, platform))}</span>
        <span>动态 {formatPriority(platformDynamicPriority(status, platform))}</span>
      </div>
      <Label>
        动态优先级
        <Input
          value={dialog?.value ?? ''}
          inputMode="numeric"
          min={0}
          placeholder="例如 1511"
          onChange={(event) => props.onValueChange(event.target.value)}
        />
      </Label>
      {props.error && <p className="formMessage error">{props.error}</p>}
    </FormDialog>
  );
}

function PriorityDemotionPopover(props: { records: PriorityDemotionRecord[] }) {
  if (!props.records.length) {
    return (
      <span className="priorityDemotionPopover" role="tooltip">
        <span className="priorityDemotionEmpty">暂无优先级降级记录</span>
      </span>
    );
  }
  return (
    <span className="priorityDemotionPopover" role="tooltip">
      <span className="priorityDemotionHeader">
        <History size={14} />
        <strong>最近 10 条优先级降级</strong>
      </span>
      {props.records.map((record) => (
        <span key={record.id} className="priorityDemotionItem">
          <span className="priorityDemotionItemHeader">
            <strong>{priorityDemotionReasonText(record)}</strong>
            {priorityIsSet(record.dynamicPriority) ? <Badge variant="secondary">队尾 {formatPriority(record.dynamicPriority)}</Badge> : null}
          </span>
          <small>{priorityDemotionMetaText(record)}</small>
          {record.errorMessage && <span className="priorityDemotionError">{record.errorMessage}</span>}
        </span>
      ))}
    </span>
  );
}

function PlatformDisabledReasonPopover(props: { record?: PlatformPolicyEvent }) {
  const record = props.record;
  if (!record) {
    return (
      <span className="priorityDemotionPopover" role="tooltip">
        <span className="priorityDemotionEmpty">暂无禁用原因记录</span>
      </span>
    );
  }
  return (
    <span className="priorityDemotionPopover" role="tooltip">
      <span className="priorityDemotionHeader">
        <History size={14} />
        <strong>禁用原因</strong>
      </span>
      <span className="priorityDemotionItem">
        <span className="priorityDemotionItemHeader">
          <strong>{platformPolicyEventReasonText(record)}</strong>
        </span>
        <small>{platformPolicyEventMetaText(record)}</small>
        {record.errorMessage && <span className="priorityDemotionError">{record.errorMessage}</span>}
      </span>
    </span>
  );
}

function platformPrioritySubtitle(status: ModelRateLimitStatus, platform: IntegrationPlatform | undefined, demotionCount: number) {
  const staticPriority = platformStaticPriority(status, platform);
  const dynamicPriority = platformDynamicPriority(status, platform);
  const base = priorityIsSet(dynamicPriority) ? `静态 ${formatPriority(staticPriority)} · 动态 ${formatPriority(dynamicPriority)}` : `静态 ${formatPriority(staticPriority)}`;
  return demotionCount ? `${base} · 降级 ${demotionCount}` : base;
}

function priorityDemotionAriaLabel(status: ModelRateLimitStatus, platform: IntegrationPlatform | undefined) {
  const count = status.recentPriorityDemotions?.length ?? 0;
  return `平台优先级 ${formatPriority(platformEffectivePriority(status, platform))}，最近 ${count} 条优先级降级记录`;
}

function priorityDemotionReasonText(record: PriorityDemotionRecord) {
  const category = record.category ? `错误分类 ${record.category}` : '';
  const code = record.errorCode ? `错误 ${record.errorCode}` : '';
  const statusCode = record.statusCode ? `状态码 ${record.statusCode}` : '';
  return [priorityDemotionReasonLabel(record.reason), statusCode, code, category].filter(Boolean).join(' · ');
}

function priorityDemotionMetaText(record: PriorityDemotionRecord) {
  const policy = priorityDemotionPolicyText(record);
  const values = [
    formatDateTime(record.createdAt),
    shortId(record.taskId) ? `任务 ${shortId(record.taskId)}` : '',
    policy,
  ].filter(Boolean);
  return values.join(' · ') || '-';
}

function priorityDemotionPolicyText(record: PriorityDemotionRecord) {
  const policyPath = [record.policySource || record.policy, record.policyRule].filter(Boolean).join('.');
  if (!policyPath) return '';
  return record.matchedValue ? `策略 ${policyPath}=${record.matchedValue}` : `策略 ${policyPath}`;
}

function priorityDemotionReasonLabel(reason: string | undefined) {
  const labels: Record<string, string> = {
    priority_demote_policy: '命中优先级降级规则',
    failover_allow_policy: '命中跨平台切换规则',
    auto_disable_policy: '命中自动禁用规则',
    client_retryable: '客户端错误可重试',
    hard_stop_policy: '命中硬拒绝规则',
    runner_policy_disabled: '全局调度策略停用',
  };
  return reason ? labels[reason] ?? reason : '优先级降级';
}

function platformPolicyEventReasonText(record: PlatformPolicyEvent) {
  const category = record.category ? `错误分类 ${record.category}` : '';
  const code = record.errorCode ? `错误 ${record.errorCode}` : '';
  const statusCode = record.statusCode ? `状态码 ${record.statusCode}` : '';
  return [platformPolicyEventReasonLabel(record), statusCode, code, category].filter(Boolean).join(' · ');
}

function platformPolicyEventMetaText(record: PlatformPolicyEvent) {
  const policyPath = [record.policySource || record.policy, record.policyRule].filter(Boolean).join('.');
  const policy = policyPath ? (record.matchedValue ? `策略 ${policyPath}=${record.matchedValue}` : `策略 ${policyPath}`) : '';
  return [
    formatDateTime(record.createdAt),
    shortId(record.taskId) ? `任务 ${shortId(record.taskId)}` : '',
    policy,
  ].filter(Boolean).join(' · ') || '-';
}

function platformPolicyEventReasonLabel(record: PlatformPolicyEvent) {
  if (record.reason) {
    return priorityDemotionReasonLabel(record.reason);
  }
  if (record.eventType === 'task.policy.auto_disabled') {
    return '命中自动禁用规则';
  }
  if (record.eventType === 'task.policy.failover_disabled') {
    return '跨平台切换时禁用';
  }
  return '平台已禁用';
}

function metricCell(metric: ModelRateLimitStatus['rpm'], includeReserved = false) {
  if (!metric.limited) return <span className="rateMetricCell"><strong>{formatLimit(metric.currentValue)} / 不限</strong><small>{includeReserved ? reservedMetricText(metric) : '未配置上限'}</small></span>;
  return (
    <span className="rateMetricCell">
      <strong>{formatLimit(metric.currentValue)} / {formatLimit(metric.limitValue)}</strong>
      <small>{includeReserved ? reservedMetricText(metric) : `窗口 ${formatPercent(metric.ratio)}`}</small>
    </span>
  );
}

function concurrencyMetricCell(status: ModelRateLimitStatus) {
  const hasAdmissionBreakdown = status.waitingSyncTasks !== undefined || status.waitingAsyncTasks !== undefined;
  const queuedTasks = hasAdmissionBreakdown
    ? (status.waitingSyncTasks ?? 0) + (status.waitingAsyncTasks ?? 0)
    : (status.queuedTasks ?? 0);
  const limitText = status.concurrent.limited ? formatLimit(status.concurrent.limitValue) : '不限';
  const queueText = status.queueLimit
    ? `${formatLimit(queuedTasks)} / ${formatLimit(status.queueLimit)}`
    : formatLimit(queuedTasks);
  return (
    <span className="rateMetricCell" title={`正在执行 / 并发上限 / 排队任务${status.oldestQueueWaitSeconds ? `；最老等待 ${Math.round(status.oldestQueueWaitSeconds)} 秒` : ''}`}>
      <strong>{formatLimit(status.concurrent.currentValue)} / {limitText} / {queueText}</strong>
      {hasAdmissionBreakdown && <small>同步 {status.waitingSyncTasks ?? 0} / 异步 {status.waitingAsyncTasks ?? 0}</small>}
    </span>
  );
}

function reservedMetricText(metric: ModelRateLimitStatus['rpm']) {
  return `已结算 ${formatLimit(metric.usedValue)} + 预占 ${formatLimit(metric.reservedValue)}`;
}

function formatDateTime(value: string | undefined) {
  if (!value) return '';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;
  const pad = (item: number) => String(item).padStart(2, '0');
  return `${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
}

function shortId(value: string | undefined) {
  if (!value) return '';
  return value.length > 8 ? value.slice(0, 8) : value;
}

function modelRuntimeStatusCell(
  status: ModelRateLimitStatus,
  platform: IntegrationPlatform | undefined,
  now: number,
  onRestore: (platformModelId: string) => Promise<void>,
  restoring: boolean,
) {
  const modelCooldownMs = cooldownRemainingMs(status.modelCooldownUntil, now);
  const platformCooldownMs = cooldownRemainingMs(status.platformCooldownUntil, now);
  const platformStatus = platform?.status || status.platformStatus || 'enabled';
  const restoreButton = runtimeRestoreButton(status, platformStatus, modelCooldownMs, platformCooldownMs, onRestore, restoring);
  if (modelCooldownMs > 0) {
    return (
      <span className="platformRuntimeStatusCell">
        <span className="platformTableName">
          <strong><Badge variant="warning">模型冷却中</Badge></strong>
          <small>剩余 {formatCooldownRemaining(modelCooldownMs)}</small>
        </span>
        {restoreButton}
      </span>
    );
  }
  if (platformStatus !== 'enabled') {
    const badge = <Badge variant="warning">已禁用</Badge>;
    return (
      <span className="platformRuntimeStatusCell">
        <AntPopover
          align={{ offset: [0, 8] }}
          content={<PlatformDisabledReasonPopover record={status.platformDisabledReason} />}
          overlayClassName="priorityDemotionAntPopover"
          placement="bottomLeft"
          trigger={['hover', 'focus']}
        >
          <span className="platformTableName" tabIndex={0}>
            <strong>{badge}</strong>
          </span>
        </AntPopover>
        {restoreButton}
      </span>
    );
  }
  if (!status.enabled) {
    return (
      <span className="platformRuntimeStatusCell">
        <span className="platformTableName">
          <strong><Badge variant="secondary">已停用</Badge></strong>
          <small>不参与路由</small>
        </span>
        {restoreButton}
      </span>
    );
  }
  if (platformCooldownMs > 0) {
    return (
      <span className="platformRuntimeStatusCell">
        <span className="platformTableName">
          <strong><Badge variant="warning">平台冷却中</Badge></strong>
          <small>剩余 {formatCooldownRemaining(platformCooldownMs)}</small>
        </span>
        {restoreButton}
      </span>
    );
  }
  return (
    <span className="platformRuntimeStatusCell">
      <span className="platformTableName">
        <strong><Badge variant="success">可用</Badge></strong>
        <small>参与路由</small>
      </span>
    </span>
  );
}

function runtimeRestoreButton(
  status: ModelRateLimitStatus,
  platformStatus: string,
  modelCooldownMs: number,
  platformCooldownMs: number,
  onRestore: (platformModelId: string) => Promise<void>,
  restoring: boolean,
) {
  const canRestore = modelCooldownMs > 0 || platformCooldownMs > 0 || platformStatus !== 'enabled' || !status.enabled;
  if (!canRestore) return null;
  return (
    <Button
      className="platformRestoreButton"
      type="button"
      variant="outline"
      size="xs"
      disabled={restoring}
      onClick={() => void onRestore(status.platformModelId)}
    >
      <RotateCcw size={13} />
      {restoring ? '恢复中' : '恢复'}
    </Button>
  );
}

function cooldownRemainingMs(cooldownUntil: string | undefined, now: number) {
  if (!cooldownUntil) return 0;
  const until = Date.parse(cooldownUntil);
  if (!Number.isFinite(until)) return 0;
  return Math.max(until - now, 0);
}

function formatCooldownRemaining(milliseconds: number) {
  const minutes = milliseconds / 60000;
  if (minutes >= 1) return `${trimNumber(Math.ceil(minutes * 10) / 10)} 分钟`;
  const seconds = Math.ceil(milliseconds / 1000);
  return `${Math.max(seconds, 1)} 秒`;
}

function formatPercent(value: number) {
  if (!Number.isFinite(value) || value <= 0) return '0%';
  return `${trimNumber(value * 100)}%`;
}

function formatLimit(value: number) {
  if (Math.abs(value) >= 10000) return `${trimNumber(value / 10000)}万`;
  if (Math.abs(value) >= 1000) return `${trimNumber(value / 1000)}k`;
  return trimNumber(value);
}

function platformEffectivePriority(status: ModelRateLimitStatus | undefined, platform: IntegrationPlatform | undefined) {
  return firstPriority(
    status?.platformEffectivePriority,
    platform?.effectivePriority,
    status?.platformDynamicPriority,
    platform?.dynamicPriority,
    status?.platformPriority,
    platform?.priority,
  );
}

function platformStaticPriority(status: ModelRateLimitStatus | undefined, platform: IntegrationPlatform | undefined) {
  return firstPriority(status?.platformPriority, platform?.priority);
}

function platformDynamicPriority(status: ModelRateLimitStatus | undefined, platform: IntegrationPlatform | undefined) {
  return firstPriority(
    status?.platformDynamicPriority,
    platform?.dynamicPriority,
    status?.platformEffectivePriority,
    platform?.effectivePriority,
    status?.platformPriority,
    platform?.priority,
  );
}

function firstPriority(...values: Array<number | undefined>) {
  return values.find(priorityIsSet);
}

function priorityIsSet(value: number | undefined): value is number {
  return typeof value === 'number' && Number.isFinite(value);
}

function formatPriority(value: number | undefined) {
  if (!Number.isFinite(value)) return '-';
  return String(Math.trunc(value ?? 0));
}

function parsePriorityInput(value: string) {
  const trimmed = value.trim();
  if (!/^\d+$/.test(trimmed)) return null;
  const parsed = Number(trimmed);
  if (!Number.isSafeInteger(parsed)) return null;
  return parsed;
}

function trimNumber(value: number) {
  return Number.isInteger(value) ? String(value) : value.toFixed(2).replace(/\.?0+$/, '');
}
