import { Badge, Button, Text } from '@mantine/core';
import { IconGavel } from '@tabler/icons-react';
import type { TFunction } from 'i18next';
import type { AdvancedColumn } from '@ema-platform/ui';
import { LICENSE_PERMISSIONS, RequirePermission } from '@ema-platform/auth';
import type { ExamAppeal } from '../../types/result';

export function examAppealsColumns(
  t: TFunction,
  locale: 'en' | 'am',
  showDate: (date: string | null | undefined) => string,
  handlers: { onDecide: (appeal: ExamAppeal) => void },
): AdvancedColumn<ExamAppeal>[] {
  return [
    {
      header: t('result.appeals.number'),
      cell: ({ row }) => (
        <Text fz="sm" ff="monospace" fw={600}>
          {row.original.appealNumber}
        </Text>
      ),
    },
    {
      header: t('result.appeals.candidate'),
      cell: ({ row }) => (
        <Text fz="sm">
          {row.original.profile
            ? `${row.original.profile.firstName} ${row.original.profile.lastName}`
            : row.original.profileId.slice(0, 8)}
        </Text>
      ),
    },
    {
      header: t('result.appeals.exam'),
      cell: ({ row }) => (
        <>
          <Text fz="sm">
            {row.original.result?.exam?.title?.[locale] ?? '—'}
          </Text>
          <Badge size="xs" variant="light" color="gray">
            {row.original.result?.status} · {row.original.result?.totalScore}
          </Badge>
        </>
      ),
    },
    {
      header: t('result.appeals.reason'),
      cell: ({ row }) => (
        <Text fz="xs" maw={300} lineClamp={3}>
          {row.original.reason}
        </Text>
      ),
    },
    {
      header: t('result.appeals.lodged'),
      cell: ({ row }) => <Text fz="xs">{showDate(row.original.createdAt)}</Text>,
    },
    {
      header: '',
      label: t('result.appeals.decide'),
      align: 'right',
      cell: ({ row }) => (
        <RequirePermission anyOf={[LICENSE_PERMISSIONS.DECIDE_EXAM_APPEAL]} hideOnly>
          <Button
            size="compact-xs"
            variant="light"
            color="grape"
            leftSection={<IconGavel size={12} />}
            onClick={() => handlers.onDecide(row.original)}
          >
            {t('result.appeals.decide')}
          </Button>
        </RequirePermission>
      ),
    },
  ];
}
