import { type StatusTone, STATUS_TONE_COLOR } from '@ema-platform/shared';
import { Badge, Box, Text } from '@mantine/core';
import type { TFunction } from 'i18next';
import type { AdvancedColumn } from '@ema-platform/ui';
import { StatusBadge } from '@ema-platform/ui';
import type { Result, ResultReviewStatus } from '../../types/result';

export const STATUS_TONE: Record<string, StatusTone> = {
  PASSED: 'success',
  FAILED: 'danger',
};

/** Where a mark sits in quality control (US-EXAM-011 → 014). */
export const REVIEW_COLOR: Record<ResultReviewStatus, string> = {
  MARKED: 'gray',
  MODERATED: 'yellow',
  APPROVED: 'blue',
  PUBLISHED: 'teal',
};

export function resultColumns(
  t: TFunction,
  locale: 'en' | 'am',
  showDate: (date: string) => string,
  getExamTitle: (id: string) => string,
): AdvancedColumn<Result>[] {
  return [
    {
      header: t('result.columns.seafarer'),
      cell: ({ row }) => (
        <Text fz="sm" fw={500}>
          {row.original.seafarer
            ? `${row.original.seafarer.firstName} ${row.original.seafarer.lastName}`
            : row.original.seafarerId.slice(0, 8)}
        </Text>
      ),
    },
    {
      header: t('result.columns.exam'),
      cell: ({ row }) => (
        <Text fz="sm">{row.original.exam ? row.original.exam.title[locale] : getExamTitle(row.original.examId)}</Text>
      ),
    },
    {
      header: t('result.columns.totalScore'),
      cell: ({ row }) => <Text fz="sm" fw={600}>{row.original.totalScore}</Text>,
    },
    {
      header: t('result.columns.status'),
      cell: ({ row }) => (
        <StatusBadge
          tone={STATUS_TONE[row.original.status]}
          label={t(`result.status.${row.original.status}`)}
          size="sm"
          variant="light"
          leftSection={
            <Box
              w={6}
              h={6}
              style={{
                borderRadius: 999,
                background: `var(--mantine-color-${STATUS_TONE_COLOR[STATUS_TONE[row.original.status]]}-6)`,
              }}
            />
          }
        />
      ),
    },
    {
      header: t('result.review.column'),
      cell: ({ row }) => (
        <Badge size="sm" variant="light" color={REVIEW_COLOR[row.original.reviewStatus] ?? 'gray'}>
          {t(`result.review.${row.original.reviewStatus}`)}
        </Badge>
      ),
    },
    {
      header: t('result.columns.date'),
      cell: ({ row }) => <Text fz="sm">{showDate(row.original.createdAt)}</Text>,
    },
  ];
}
