import { Badge, Text, Tooltip } from '@mantine/core';
import type { TFunction } from 'i18next';
import type { LicenseType } from '@ema-platform/api';
import type { AdvancedColumn } from '@ema-platform/ui';

function feeText(amount: string | number | null, currency: string): string {
  if (amount === null || amount === '') return '—';
  const value = Number(amount);
  if (!Number.isFinite(value)) return '—';
  return `${value.toLocaleString('en-US')} ${currency}`;
}

export function paymentConfigColumns(
  t: TFunction,
  localized: (value: LicenseType['name']) => string,
): AdvancedColumn<LicenseType>[] {
  return [
    {
      header: t('paymentConfig.columns.type', 'Licence type'),
      cell: ({ row }) => (
        <>
          <Text size="sm" fw={600}>
            {localized(row.original.name)}
          </Text>
          <Text size="xs" c="dimmed" ff="monospace">
            {row.original.key}
          </Text>
        </>
      ),
    },
    {
      header: t('paymentConfig.columns.newApplication', 'New application'),
      cell: ({ row }) => (
        <Text size="sm" fw={500}>
          {feeText(row.original.feeNewApplication, row.original.feeCurrency)}
        </Text>
      ),
    },
    {
      header: t('paymentConfig.columns.renewal', 'Renewal'),
      cell: ({ row }) => {
        const type = row.original;
        if (type.feeNewApplication === null) {
          // No charge at all, so "same as new" would be noise.
          return (
            <Text size="sm" c="dimmed">
              —
            </Text>
          );
        }
        if (type.feeRenewal === null) {
          return (
            <Tooltip
              label={t(
                'paymentConfig.renewalSameTooltip',
                'No separate renewal fee — renewal is charged at the new-application rate',
              )}
            >
              <Text size="sm" c="dimmed">
                {feeText(type.feeNewApplication, type.feeCurrency)}{' '}
                <Text span size="xs" c="dimmed">
                  {t('paymentConfig.renewalSameAsNew', '(same as new)')}
                </Text>
              </Text>
            </Tooltip>
          );
        }
        return (
          <Text size="sm" fw={500}>
            {feeText(type.feeRenewal, type.feeCurrency)}
          </Text>
        );
      },
    },
    {
      header: t('paymentConfig.columns.charged', 'Charged?'),
      cell: ({ row }) =>
        row.original.issuesCertificate ? (
          <Badge variant="light" color="teal" size="sm">
            {t('paymentConfig.chargedOnApproval', 'On approval')}
          </Badge>
        ) : (
          <Tooltip
            label={t(
              'paymentConfig.chargedNotChargedTooltip',
              'This licence type ends with an EMA decision and never reaches a payment stage',
            )}
          >
            <Badge variant="light" color="gray" size="sm">
              {t('paymentConfig.chargedNotCharged', 'Not charged')}
            </Badge>
          </Tooltip>
        ),
    },
  ];
}
