import { Badge, Button, Text } from '@mantine/core';
import { IconFileText } from '@tabler/icons-react';
import type { TFunction } from 'i18next';
import type { AdvancedColumn } from '@ema-platform/ui';
import type { Bilingual, IssuedLicense } from '@ema-platform/api';
import { PORTAL_PERMISSIONS } from '@ema-platform/auth';

export function waiverLetterColumns(deps: {
  t: TFunction;
  /** Permission check from usePermissions() — hooks can't run in a cell. */
  can: (required?: string[]) => boolean;
  localized: (value: Bilingual | undefined) => string;
  showDate: (value: string | null | undefined) => string;
  onDownload: (license: IssuedLicense) => void;
}): AdvancedColumn<IssuedLicense>[] {
  const { t } = deps;
  return [
    {
      header: t('waiver.columns.reference', 'Reference'),
      cell: ({ row }) => (
        <Text ff="monospace" size="sm" fw={600}>
          {row.original.certificateNumber}
        </Text>
      ),
    },
    {
      header: t('waiver.columns.kind', 'Kind'),
      cell: ({ row }) => deps.localized(row.original.licenseType?.name) || '—',
    },
    {
      header: t('waiver.columns.issued', 'Issued'),
      cell: ({ row }) => deps.showDate(row.original.issueDate),
    },
    {
      header: t('waiver.columns.status', 'Status'),
      cell: ({ row }) => (
        <Badge
          size="sm"
          variant="light"
          color={row.original.status === 'ACTIVE' ? 'green' : 'red'}
        >
          {t(`waiver.licenseStatus.${row.original.status}`, row.original.status)}
        </Badge>
      ),
    },
    {
      header: '',
      label: t('waiver.columns.actions', 'Actions'),
      align: 'right',
      cell: ({ row }) =>
        deps.can([PORTAL_PERMISSIONS.VIEW_WAIVER_LETTER]) ? (
          <Button
            size="compact-xs"
            variant="light"
            leftSection={<IconFileText size={13} />}
            onClick={() => deps.onDownload(row.original)}
          >
            {t('waiver.letter', 'Letter')}
          </Button>
        ) : null,
    },
  ];
}
