import { type StatusTone } from '@ema-platform/shared';
import { Button, Text, Tooltip } from '@mantine/core';
import { IconShieldCog } from '@tabler/icons-react';
import type { AdvancedColumn } from '@ema-platform/ui';
import { StatusBadge } from '@ema-platform/ui';
import type { Bilingual, IssuedLicense } from '@ema-platform/api';
import { LICENSE_PERMISSIONS, RequirePermission } from '@ema-platform/auth';

const LICENSE_STATUS_TONES: Record<string, StatusTone> = {
  ACTIVE: 'success',
  EXPIRED: 'warning',
  SUSPENDED: 'pending',
  CANCELLED: 'danger',
  SUPERSEDED: 'neutral',
};

export type LifecycleAction = 'suspend' | 'revoke' | 'reinstate';

/** Which lifecycle actions make sense from each current status. */
export function actionsFor(license: IssuedLicense): LifecycleAction[] {
  switch (license.status) {
    case 'ACTIVE':
    case 'EXPIRED':
      return ['suspend', 'revoke'];
    case 'SUSPENDED':
      return ['reinstate', 'revoke'];
    default:
      return [];
  }
}

export function licenseRegisterColumns(
  localized: (value: Bilingual | undefined) => string,
  showDate: (date: string | null | undefined) => string,
  handlers: { onStatus: (license: IssuedLicense) => void },
): AdvancedColumn<IssuedLicense>[] {
  return [
    {
      header: 'Certificate №',
      cell: ({ row }) => (
        <Text size="sm" ff="monospace" fw={600}>
          {row.original.certificateNumber}
        </Text>
      ),
    },
    {
      header: 'Type',
      cell: ({ row }) => (
        <Text size="sm">{localized(row.original.licenseType?.name)}</Text>
      ),
    },
    {
      header: 'Holder',
      cell: ({ row }) => <Text size="sm">{row.original.companyName ?? '—'}</Text>,
    },
    {
      header: 'Issued',
      cell: ({ row }) => (
        <Text size="sm" c="dimmed">
          {showDate(row.original.issueDate)}
        </Text>
      ),
    },
    {
      header: 'Expires',
      cell: ({ row }) => (
        <Text size="sm" c="dimmed">
          {showDate(row.original.expiryDate)}
        </Text>
      ),
    },
    {
      header: 'Status',
      cell: ({ row }) => (
        <StatusBadge
          tone={LICENSE_STATUS_TONES[row.original.status] ?? 'neutral'}
          label={row.original.status}
          size="sm"
          variant="light"
        />
      ),
    },
    {
      header: '',
      label: 'Actions',
      align: 'right',
      cell: ({ row }) =>
        actionsFor(row.original).length > 0 ? (
          <RequirePermission
            anyOf={[
              LICENSE_PERMISSIONS.SUSPEND_LICENSE,
              LICENSE_PERMISSIONS.CANCEL_LICENSE,
            ]}
            hideOnly
          >
            <Tooltip label="Suspend / revoke / reinstate">
              <Button
                size="compact-xs"
                variant="subtle"
                leftSection={<IconShieldCog size={14} />}
                onClick={() => handlers.onStatus(row.original)}
              >
                Status
              </Button>
            </Tooltip>
          </RequirePermission>
        ) : null,
    },
  ];
}
