import type { AgentRunStatus } from "@/types/api";

export function formatDateTime(value?: string) {
  if (!value) return "刚刚";
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;

  const pad = (input: number) => String(input).padStart(2, "0");
  return `${date.getMonth() + 1}/${date.getDate()} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}

export function statusText(status?: AgentRunStatus) {
  const normalizedStatus = String(status || "").toUpperCase();

  switch (normalizedStatus) {
    case "PENDING":
      return "排队中";
    case "RUNNING":
      return "制作中";
    case "PAUSED":
      return "已暂停";
    case "DONE":
      return "已完成";
    case "FAILED":
      return "失败";
    case "DRAFT":
      return "草稿";
    case "CREATED":
      return "已创建";
    case "SKIPPED":
      return "已跳过";
    default:
      return status || "未开始";
  }
}

export function statusClass(status?: AgentRunStatus) {
  const normalizedStatus = String(status || "").toUpperCase();

  switch (normalizedStatus) {
    case "RUNNING":
      return "status-running";
    case "DONE":
    case "SKIPPED":
      return "status-done";
    case "FAILED":
      return "status-failed";
    case "PAUSED":
      return "status-paused";
    default:
      return "status-pending";
  }
}

export function stepProgress(statuses: Array<AgentRunStatus | undefined>) {
  if (!statuses.length) return 0;
  const done = statuses.filter((status) => status === "DONE" || status === "SKIPPED").length;
  return Math.round((done / statuses.length) * 100);
}
