import type { TaskRuntimeStatus } from '@/lib/task-runtime';
import type { ProcessArtifact, ProcessRun } from '@/types';
import { getCurrentAppLocale, pickAppText, type AppLocale } from '@/lib/i18n/core';
import type { WsStatus } from '@/lib/api';

export function appStatusLabel(
  status: ProcessRun['status'] | TaskRuntimeStatus | string,
  locale: AppLocale = getCurrentAppLocale()
): string {
  if (status === 'queued') return pickAppText(locale, '排队中', 'Queued');
  if (status === 'running') return pickAppText(locale, '运行中', 'Running');
  if (status === 'waiting') return pickAppText(locale, '等待中', 'Waiting');
  if (status === 'blocked') return pickAppText(locale, '阻塞', 'Blocked');
  if (status === 'done') return pickAppText(locale, '已完成', 'Done');
  if (status === 'error') return pickAppText(locale, '失败', 'Error');
  if (status === 'cancelled') return pickAppText(locale, '已取消', 'Cancelled');
  return status;
}

export function appActorTypeLabel(actorType: string, locale: AppLocale = getCurrentAppLocale()): string {
  if (actorType === 'mcp') return 'MCP';
  if (actorType === 'system') return pickAppText(locale, '系统', 'System');
  if (actorType === 'agent') return pickAppText(locale, '智能体', 'Agent');
  return actorType;
}

export function appEventKindLabel(kind: string, locale: AppLocale = getCurrentAppLocale()): string {
  if (kind === 'run_started') return pickAppText(locale, '已启动', 'Started');
  if (kind === 'run_progress') return pickAppText(locale, '进行中', 'In Progress');
  if (kind === 'run_status') return pickAppText(locale, '状态更新', 'Status');
  if (kind === 'run_message') return pickAppText(locale, '消息', 'Message');
  if (kind === 'run_artifact') return pickAppText(locale, '产物', 'Artifact');
  if (kind === 'run_finished') return pickAppText(locale, '已结束', 'Finished');
  if (kind === 'run_cancelled') return pickAppText(locale, '已取消', 'Cancelled');
  return kind;
}

export function appFeedRoleLabel(
  role: 'user' | 'assistant' | 'system' | 'tool',
  locale: AppLocale = getCurrentAppLocale()
): string {
  if (role === 'user') return pickAppText(locale, '主 agent', 'Lead agent');
  if (role === 'tool') return pickAppText(locale, '工具输出', 'Tool output');
  if (role === 'system') return pickAppText(locale, '状态', 'Status');
  return pickAppText(locale, '子 agent', 'Sub-agent');
}

export function appArtifactPreview(artifact: ProcessArtifact, locale: AppLocale = getCurrentAppLocale()): string {
  if (artifact.artifact_type === 'link' && artifact.url) {
    return `${artifact.title}\n${artifact.url}`;
  }
  if ((artifact.artifact_type === 'text' || artifact.artifact_type === 'markdown') && artifact.content) {
    return `${artifact.title}\n${artifact.content}`;
  }
  if (artifact.artifact_type === 'json') {
    return `${artifact.title}\n${pickAppText(locale, '已生成结构化结果', 'Structured output generated')}`;
  }
  if (artifact.file_id) {
    return `${artifact.title}\n${pickAppText(locale, '已生成文件输出', 'File output generated')}`;
  }
  return artifact.title;
}

export function appConnectionStatusLabel(
  wsStatus: WsStatus,
  beaverReady: boolean | null,
  locale: AppLocale = getCurrentAppLocale()
): string {
  const isOnline = wsStatus === 'connected' && beaverReady === true;
  const isChecking = wsStatus === 'connected' && beaverReady === null;
  const isOffline = wsStatus === 'disconnected' || (wsStatus === 'connected' && beaverReady === false);

  if (isOnline) return pickAppText(locale, '已连接', 'Connected');
  if (isChecking) return pickAppText(locale, '检查中', 'Checking');
  if (wsStatus === 'connecting') return pickAppText(locale, '连接中', 'Connecting');
  if (isOffline && wsStatus === 'connected') return pickAppText(locale, '服务离线', 'Service offline');
  return pickAppText(locale, '未连接', 'Disconnected');
}
