import React from 'react';
import { Tag, Tooltip } from 'antd';

interface LabelMeta {
  text: string;
  color?: string;
}

const STATUS_LABELS: Record<string, LabelMeta> = {
  // 通用任务状态
  pending: { text: '待处理', color: 'default' },
  waiting_user: { text: '等待用户操作', color: 'processing' },
  processing: { text: '处理中', color: 'warning' },
  completed: { text: '已完成', color: 'success' },
  failed: { text: '失败', color: 'error' },
  cancelled: { text: '已取消', color: 'default' },
  canceled: { text: '已取消', color: 'default' },
  timeout: { text: '已超时', color: 'error' },
  queued: { text: '已入队', color: 'processing' },
  preparing: { text: '准备中', color: 'processing' },

  // 生成任务 pipeline / download stage
  creating_provider_task: { text: '创建远端任务', color: 'processing' },
  waiting_remote: { text: '等待远端结果', color: 'processing' },
  polling: { text: '轮询远端结果', color: 'processing' },
  result_ready: { text: '结果已就绪', color: 'success' },
  downloading: { text: '下载中', color: 'processing' },
  done: { text: '已完成', color: 'success' },
  download_failed: { text: '下载失败', color: 'error' },
  retry_waiting: { text: '等待重试', color: 'orange' },

  // 拆镜总任务状态
  pending_analysis: { text: '等待分析', color: 'default' },
  analyzing: { text: '分析中', color: 'processing' },
  analysis_completed: { text: '分析完成', color: 'success' },
  analysis_failed: { text: '分析失败', color: 'error' },
  splitting: { text: '拆镜中', color: 'warning' },
  split_completed: { text: '拆镜完成', color: 'success' },
  partial_failed: { text: '部分失败', color: 'orange' },

  // 拆镜片段状态
  none: { text: '未拆镜', color: 'default' },
  not_required: { text: '无需处理', color: 'default' },
  not_started: { text: '未开始', color: 'default' },
  project_created: { text: '已创建项目', color: 'processing' },

  // 布尔/兜底类
  true: { text: '是', color: 'success' },
  false: { text: '否', color: 'default' },
};

const STEP_LABELS: Record<string, string> = {
  material_input: '素材输入',
  image_prompt_optimize: '图片 AI 提词',
  image_generate: '图片生成',
  video_prompt_optimize: '视频 AI 提词',
  video_generate: '视频生成',
};

const MODULE_LABELS: Record<string, string> = {
  hot_opening_replicate: '爆款开头复刻',
  shot_replicate: '拆镜复刻',
};

const SOURCE_MODE_LABELS: Record<string, LabelMeta> = {
  ai_suggestion: { text: 'AI 建议', color: 'purple' },
  custom: { text: '自定义', color: 'cyan' },
  original: { text: '原视频', color: 'blue' },
};

export function getModuleLabel(value?: string | null): string {
  if (!value) return '-';
  return MODULE_LABELS[value] || value;
}

export function getStepCodeLabel(value?: string | null): string {
  if (!value) return '-';
  return STEP_LABELS[value] || value;
}

export function getStatusLabel(value?: string | null): string {
  if (!value) return '-';
  return STATUS_LABELS[value]?.text || STEP_LABELS[value] || MODULE_LABELS[value] || SOURCE_MODE_LABELS[value]?.text || value;
}

export function getStatusColor(value?: string | null): string {
  if (!value) return 'default';
  return STATUS_LABELS[value]?.color || SOURCE_MODE_LABELS[value]?.color || 'blue';
}

interface StatusTagProps {
  status?: string | boolean | null;
  tooltipRaw?: boolean;
}

const StatusTag: React.FC<StatusTagProps> = ({ status, tooltipRaw = true }) => {
  if (status === undefined || status === null || status === '') return <Tag>-</Tag>;
  const raw = String(status);
  const label = getStatusLabel(raw);
  const tag = <Tag color={getStatusColor(raw)}>{label}</Tag>;
  if (!tooltipRaw || label === raw) return tag;
  return <Tooltip title={raw}>{tag}</Tooltip>;
};

export default StatusTag;
