import { Badge, Group, Text, Tooltip } from '@mantine/core';
import type { AdvancedColumn } from '@ema-platform/ui';
import type { MedicalCertificate, SeaServiceRecord } from '@ema-platform/api';

const RECORD_STATUS_COLORS: Record<string, string> = {
  SUBMITTED: 'blue',
  VERIFIED: 'green',
  REJECTED: 'red',
};

export const FITNESS_OPTIONS = [
  { value: 'FIT', label: 'Fit' },
  { value: 'FIT_WITH_RESTRICTIONS', label: 'Fit with restrictions' },
  { value: 'UNFIT', label: 'Unfit' },
];

export function seaServiceColumns(
  showDate: (date: string) => string,
): AdvancedColumn<SeaServiceRecord>[] {
  return [
    {
      header: 'Vessel',
      cell: ({ row }) => (
        <>
          <Text fw={600} size="sm">
            {row.original.vesselName}
          </Text>
          {row.original.imoNumber && (
            <Text size="xs" c="dimmed">
              IMO {row.original.imoNumber}
            </Text>
          )}
        </>
      ),
    },
    { header: 'Rank', accessorKey: 'rank' },
    {
      header: 'From',
      accessorKey: 'engagementDate',
      cell: ({ row }) => showDate(row.original.engagementDate),
    },
    {
      header: 'To',
      accessorKey: 'dischargeDate',
      cell: ({ row }) => showDate(row.original.dischargeDate),
    },
    {
      header: 'Status',
      cell: ({ row }) => (
        <Tooltip
          label={row.original.verificationRemark ?? ''}
          disabled={!row.original.verificationRemark}
        >
          <Badge color={RECORD_STATUS_COLORS[row.original.status]}>
            {row.original.status}
          </Badge>
        </Tooltip>
      ),
    },
  ];
}

export function medicalColumns(
  showDate: (date: string) => string,
): AdvancedColumn<MedicalCertificate>[] {
  const today = new Date().toISOString().slice(0, 10);
  return [
    {
      header: 'Issuer',
      cell: ({ row }) => (
        <>
          <Text fw={600} size="sm">
            {row.original.issuerName}
          </Text>
          {row.original.certificateNumber && (
            <Text size="xs" c="dimmed">
              № {row.original.certificateNumber}
            </Text>
          )}
        </>
      ),
    },
    {
      header: 'Issued',
      accessorKey: 'issueDate',
      cell: ({ row }) => showDate(row.original.issueDate),
    },
    {
      header: 'Expires',
      cell: ({ row }) => (
        <Group gap={6} wrap="nowrap">
          {showDate(row.original.expiryDate)}
          {row.original.expiryDate < today && <Badge color="red">Expired</Badge>}
        </Group>
      ),
    },
    {
      header: 'Fitness',
      cell: ({ row }) =>
        FITNESS_OPTIONS.find((o) => o.value === row.original.fitnessStatus)
          ?.label ?? row.original.fitnessStatus,
    },
    {
      header: 'Status',
      cell: ({ row }) => (
        <Tooltip
          label={row.original.verificationRemark ?? ''}
          disabled={!row.original.verificationRemark}
        >
          <Badge color={RECORD_STATUS_COLORS[row.original.status]}>
            {row.original.status}
          </Badge>
        </Tooltip>
      ),
    },
  ];
}
