import { Button } from '@mantine/core';
import type { TFunction } from 'i18next';
import type { LicenseApplication } from '@ema-platform/api';

interface Props {
  app: LicenseApplication;
  t: TFunction;
  requesting: boolean;
  paying: boolean;
  onRequestExamFee: (app: LicenseApplication) => void;
  onPay: (app: LicenseApplication) => void;
}

/**
 * What the candidate can do while an examined certificate is in its exam leg.
 *
 * Kept apart from the general actions column because these statuses only ever
 * occur on types that examine — folding them into that column would put five
 * more branches into a cell that already reads as a chain of ternaries.
 *
 * Returns null for every other status, so the caller can render it
 * unconditionally.
 */
export function ExamStageActions({
  app,
  t,
  requesting,
  paying,
  onRequestExamFee,
  onPay,
}: Props) {
  // Eligible but not yet committed to sitting, or sat and not passed: both are
  // the same decision — ask for the fee that buys a sitting.
  if (app.status === 'ELIGIBILITY_APPROVED' || app.status === 'EXAM_FAILED') {
    const retake = app.status === 'EXAM_FAILED';
    return (
      <Button
        size="xs"
        variant="filled"
        color={retake ? 'orange' : 'teal'}
        loading={requesting}
        onClick={() => onRequestExamFee(app)}
      >
        {retake
          ? t('applications.actions.bookRetake', 'Book a resit')
          : t('applications.actions.bookExam', 'Book exam')}
      </Button>
    );
  }

  if (app.status === 'EXAM_PAYMENT_PENDING') {
    return (
      <Button
        size="xs"
        variant="filled"
        color="yellow"
        loading={paying}
        onClick={() => onPay(app)}
      >
        {t('applications.actions.payExamFee', {
          defaultValue: 'Pay exam fee ({{amount}} {{currency}})',
          amount: Number(app.feeAmount ?? 0).toLocaleString(),
          currency: app.feeCurrency,
        })}
      </Button>
    );
  }

  // Paid and scheduled are both waiting states — nothing for the candidate to
  // do, so say so rather than offering a button that does nothing.
  if (app.status === 'EXAM_PAID' || app.status === 'EXAM_SCHEDULED') {
    return (
      <Button size="xs" variant="subtle" disabled>
        {app.status === 'EXAM_PAID'
          ? t('applications.actions.awaitingDate', 'Awaiting exam date')
          : t('applications.actions.examScheduled', 'Exam scheduled')}
      </Button>
    );
  }

  return null;
}
