import type { PrimaryTableCellParams, PrimaryTableCol } from "tdesign-react";

import type { CheckResult } from "../../shared/api";

import { StatusDot } from "../components/StatusDot";

export const HISTORY_COLUMNS: Array<PrimaryTableCol<CheckResult>> = [
  {
    cell: ({ row }: PrimaryTableCellParams<CheckResult>) => <StatusDot up={!!row.matched} />,
    colKey: "matched",
    title: "#",
    width: 40,
  },
  {
    cell: ({ row }: PrimaryTableCellParams<CheckResult>) => formatTimestamp(row.timestamp),
    colKey: "timestamp",
    title: "时间",
    width: 180,
  },
  {
    align: "center",
    cell: ({ row }: PrimaryTableCellParams<CheckResult>) =>
      row.durationMs !== null ? Math.round(row.durationMs) : "-",
    colKey: "durationMs",
    title: "耗时(ms)",
    width: 96,
  },
  {
    cell: ({ row }: PrimaryTableCellParams<CheckResult>) => {
      const parts = [row.detail, row.failure?.message].filter(Boolean);
      return parts.length > 0 ? parts.join("：") : "-";
    },
    colKey: "detail",
    title: "详情",
  },
];

function formatTimestamp(timestamp: string): string {
  const date = new Date(timestamp);
  const pad = (value: number) => String(value).padStart(2, "0");
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
}
