import { type StatusTone } from '@ema-platform/shared';
import { Badge, Text } from "@mantine/core";
import type { TFunction } from "i18next";
import type { AdvancedColumn } from "@ema-platform/ui";
import { StatusBadge } from '@ema-platform/ui';
import type { Exam } from "../../types/exam";

const STATUS_TONE: Record<string, StatusTone> = {
  PENDING: 'neutral',
  ACTIVE: 'info',
  COMPLETED: 'success',
  CANCELLED: 'danger',
  POSTPONED: 'pending',
  PUBLISHED: 'success',
};

export function examColumns(
  t: TFunction,
  locale: "en" | "am",
  getCertName: (id: string) => string,
  onTitleClick: (exam: Exam) => void,
): AdvancedColumn<Exam>[] {
  return [
    {
      header: t("exam.columns.title"),
      cell: ({ row }) => (
        <Text
          fz="sm"
          fw={500}
          c="blue"
          style={{ cursor: "pointer" }}
          onClick={() => onTitleClick(row.original)}
        >
          {row.original.title[locale]}
        </Text>
      ),
    },
    {
      header: t("exam.columns.certification"),
      cell: ({ row }) => <Text fz="sm">{getCertName(row.original.certificationId)}</Text>,
    },
    {
      header: t("exam.columns.date"),
      cell: ({ row }) => <Text fz="sm">{row.original.date}</Text>,
    },
    {
      header: t("exam.columns.type"),
      cell: ({ row }) => (
        <Badge size="sm" variant="light" color={row.original.type === "WRITTEN" ? "blue" : "orange"}>
          {t(`exam.type.${row.original.type}`)}
        </Badge>
      ),
    },
    {
      header: t("exam.columns.form"),
      cell: ({ row }) => (
        <Badge size="sm" variant="light" color={row.original.form === "ESSAY" ? "blue" : "violet"}>
          {t(`exam.formType.${row.original.form}`)}
        </Badge>
      ),
    },
    {
      header: t("exam.columns.venue"),
      cell: ({ row }) => <Text fz="sm">{row.original.venue}</Text>,
    },
    {
      header: t("exam.columns.questions"),
      cell: ({ row }) => (
        <Badge size="sm" variant="light" color="gray">
          {row.original.questions?.length ?? 0}
        </Badge>
      ),
    },
    {
      header: t("exam.columns.status"),
      cell: ({ row }) => (
        <StatusBadge
          tone={STATUS_TONE[row.original.status]}
          label={t(`exam.status.${row.original.status}`)}
          size="sm"
          variant="light"
        />
      ),
    },
  ];
}
