import { ActionIcon, Group, Tooltip } from '@mantine/core';
import { IconEdit, IconPaperclip, IconTrash } from '@tabler/icons-react';
import type { TFunction } from 'i18next';
import type { AdvancedColumn } from '@ema-platform/ui';
import type { MedicalCertificate, SeaServiceRecord } from '@ema-platform/api';
import { PORTAL_PERMISSIONS } from '@ema-platform/auth';

export function seaServiceActionsColumn(
  t: TFunction,
  handlers: {
    /** Permission check from usePermissions() — hooks can't run in a cell. */
    can: (required?: string[]) => boolean;
    onEvidence: (record: SeaServiceRecord) => void;
    onEdit: (record: SeaServiceRecord) => void;
    onDelete: (record: SeaServiceRecord) => void;
  },
): AdvancedColumn<SeaServiceRecord> {
  return {
    header: '',
    label: t('common.actions'),
    align: 'right',
    cell: ({ row }) => {
      const record = row.original;
      const locked = record.status !== 'SUBMITTED';
      return (
        <Group gap="xs" justify="flex-end" wrap="nowrap">
          <Tooltip label={t('seaRecords.actions.evidence')}>
            <ActionIcon
              variant="subtle"
              onClick={() => handlers.onEvidence(record)}
            >
              <IconPaperclip size={16} />
            </ActionIcon>
          </Tooltip>
          {handlers.can([PORTAL_PERMISSIONS.EDIT_SEA_SERVICE]) && (
            <>
              <Tooltip label={locked ? t('seaRecords.actions.frozen') : t('seaRecords.actions.edit')}>
                <ActionIcon
                  variant="subtle"
                  disabled={locked}
                  onClick={() => handlers.onEdit(record)}
                >
                  <IconEdit size={16} />
                </ActionIcon>
              </Tooltip>
              <Tooltip label={locked ? t('seaRecords.actions.frozen') : t('seaRecords.actions.delete')}>
                <ActionIcon
                  variant="subtle"
                  color="red"
                  disabled={locked}
                  onClick={() => handlers.onDelete(record)}
                >
                  <IconTrash size={16} />
                </ActionIcon>
              </Tooltip>
            </>
          )}
        </Group>
      );
    },
  };
}

export function medicalActionsColumn(
  t: TFunction,
  handlers: {
    /** Permission check from usePermissions() — hooks can't run in a cell. */
    can: (required?: string[]) => boolean;
    onEvidence: (certificate: MedicalCertificate) => void;
    onEdit: (certificate: MedicalCertificate) => void;
    onDelete: (certificate: MedicalCertificate) => void;
  },
): AdvancedColumn<MedicalCertificate> {
  return {
    header: '',
    label: t('common.actions'),
    align: 'right',
    cell: ({ row }) => {
      const certificate = row.original;
      const locked = certificate.status !== 'SUBMITTED';
      return (
        <Group gap="xs" justify="flex-end" wrap="nowrap">
          <Tooltip label={t('seaRecords.actions.scanEvidence')}>
            <ActionIcon
              variant="subtle"
              onClick={() => handlers.onEvidence(certificate)}
            >
              <IconPaperclip size={16} />
            </ActionIcon>
          </Tooltip>
          {handlers.can([PORTAL_PERMISSIONS.UPLOAD_MEDICAL]) && (
            <>
              <Tooltip
                label={locked ? t('seaRecords.actions.certificatesFrozen') : t('seaRecords.actions.edit')}
              >
                <ActionIcon
                  variant="subtle"
                  disabled={locked}
                  onClick={() => handlers.onEdit(certificate)}
                >
                  <IconEdit size={16} />
                </ActionIcon>
              </Tooltip>
              <Tooltip
                label={locked ? t('seaRecords.actions.certificatesFrozen') : t('seaRecords.actions.delete')}
              >
                <ActionIcon
                  variant="subtle"
                  color="red"
                  disabled={locked}
                  onClick={() => handlers.onDelete(certificate)}
                >
                  <IconTrash size={16} />
                </ActionIcon>
              </Tooltip>
            </>
          )}
        </Group>
      );
    },
  };
}
