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;
  onRetakeExam: (app: LicenseApplication) => void;
  onPay: (app: LicenseApplication) => void;
  /** Opens the exams screen, where sittings are listed and registered for. */
  onRegisterForExam: () => void;
}

/**
 * What the candidate can do while an examined certificate is in its
 * eligibility or 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,
  onRetakeExam,
  onPay,
  onRegisterForExam,
}: Props) {
  // The eligibility fee is invoiced the moment the application is submitted —
  // there is no separate "request" step, so this is a pay button, exactly
  // like EXAM_PAYMENT_PENDING below.
  if (app.status === 'ELIGIBILITY_PAYMENT_PENDING') {
    return (
      <Button
        size="xs"
        variant="filled"
        color="yellow"
        loading={paying}
        onClick={() => onPay(app)}
      >
        {t('applications.actions.payEligibilityFee', {
          defaultValue: 'Pay eligibility fee ({{amount}} {{currency}})',
          amount: Number(app.feeAmount ?? 0).toLocaleString(),
          currency: app.feeCurrency,
        })}
      </Button>
    );
  }

  // Paid — queued for backoffice review. Nothing for the candidate to do.
  if (app.status === 'ELIGIBILITY_PAID') {
    return (
      <Button size="xs" variant="subtle" disabled>
        {t('applications.actions.eligibilityUnderReview', 'Under review')}
      </Button>
    );
  }

  // Failed a sitting: the only decision left is whether to pay for another.
  if (app.status === 'EXAM_FAILED') {
    return (
      <Button
        size="xs"
        variant="filled"
        color="orange"
        loading={requesting}
        onClick={() => onRetakeExam(app)}
      >
        {t('applications.actions.bookRetake', 'Book a resit')}
      </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>
    );
  }

  // Passed: the certificate itself is the last fee on the application.
  if (app.status === 'EXAM_PASSED') {
    return (
      <Button
        size="xs"
        variant="filled"
        color="yellow"
        loading={paying}
        onClick={() => onPay(app)}
      >
        {t('applications.actions.payCertificateFee', {
          defaultValue: 'Pay certificate fee ({{amount}} {{currency}})',
          amount: Number(app.feeAmount ?? 0).toLocaleString(),
          currency: app.feeCurrency,
        })}
      </Button>
    );
  }

  // Not a waiting state any more. Nobody seats the candidate: the exam fee
  // having cleared is precisely what makes them eligible to pick a published
  // sitting, so this is the one thing left for them to do.
  if (app.status === 'EXAM_PAID') {
    return (
      <Button
        size="xs"
        variant="filled"
        color="cyan"
        onClick={onRegisterForExam}
      >
        {t('applications.actions.registerForExam', 'Register for a sitting')}
      </Button>
    );
  }

  // Registered. What happens next is the invigilator's, then the examiner's —
  // the exams screen is where the candidate watches it, and where Take Exam
  // appears once attendance is confirmed.
  if (app.status === 'EXAM_SCHEDULED') {
    return (
      <Button size="xs" variant="light" onClick={onRegisterForExam}>
        {t('applications.actions.viewSitting', 'View my sitting')}
      </Button>
    );
  }

  return null;
}
