import { Badge, Button, Text } from '@mantine/core';
import { IconFileText, IconGavel } from '@tabler/icons-react';
import type { AdvancedColumn } from '@ema-platform/ui';
import type { Bilingual } from '@ema-platform/api';
import { PORTAL_PERMISSIONS } from '@ema-platform/auth';
import type {
  AttendanceStatus,
  MyAppeal,
  MyRegistration,
  MyResult,
} from './index';

const ATTENDANCE_COLOR: Record<AttendanceStatus, string> = {
  REGISTERED: 'gray',
  PRESENT: 'teal',
  LATE: 'yellow',
  ABSENT: 'red',
  WITHDRAWN: 'orange',
  DISQUALIFIED: 'red',
};

export function registrationColumns(deps: {
  /** Permission check from usePermissions() — hooks can't run in a cell. */
  can: (required?: string[]) => boolean;
  localized: (value: Bilingual | undefined) => string;
  showDate: (value: string | null | undefined) => string;
  onDownloadSlip: (registration: MyRegistration) => void;
}): AdvancedColumn<MyRegistration>[] {
  return [
    {
      header: 'Admission №',
      cell: ({ row }) => (
        <Text ff="monospace" size="sm" fw={600}>
          {row.original.admissionNumber}
        </Text>
      ),
    },
    {
      header: 'Examination',
      cell: ({ row }) => deps.localized(row.original.exam?.title) || '—',
    },
    {
      header: 'Date',
      cell: ({ row }) => deps.showDate(row.original.exam?.date),
    },
    {
      header: 'Venue',
      cell: ({ row }) => row.original.exam?.venue ?? '—',
    },
    {
      header: 'Attempt',
      cell: ({ row }) => (
        <Badge
          size="sm"
          variant="light"
          color={row.original.kind === 'RETAKE' ? 'orange' : 'blue'}
        >
          {row.original.kind === 'RETAKE'
            ? `Retake · ${row.original.attemptNumber}`
            : 'First sitting'}
        </Badge>
      ),
    },
    {
      header: 'Attendance',
      cell: ({ row }) => (
        <Badge
          size="sm"
          variant="light"
          color={ATTENDANCE_COLOR[row.original.attendanceStatus] ?? 'gray'}
        >
          {row.original.attendanceStatus}
        </Badge>
      ),
    },
    {
      header: 'Slip',
      cell: ({ row }) =>
        deps.can([PORTAL_PERMISSIONS.VIEW_OWN_EXAM]) ? (
          <Button
            size="compact-xs"
            variant="light"
            leftSection={<IconFileText size={13} />}
            onClick={() => deps.onDownloadSlip(row.original)}
          >
            Slip
          </Button>
        ) : null,
    },
  ];
}

export function resultColumns(deps: {
  /** Permission check from usePermissions() — hooks can't run in a cell. */
  can: (required?: string[]) => boolean;
  localized: (value: Bilingual | undefined) => string;
  showDate: (value: string | null | undefined) => string;
  appeals: MyAppeal[];
  onAppeal: (result: MyResult) => void;
}): AdvancedColumn<MyResult>[] {
  return [
    {
      header: 'Examination',
      cell: ({ row }) => deps.localized(row.original.exam?.title) || '—',
    },
    {
      header: 'Published',
      cell: ({ row }) => deps.showDate(row.original.publishedAt),
    },
    {
      header: 'Score',
      cell: ({ row }) => (
        <Text fw={600} size="sm">
          {row.original.totalScore}
        </Text>
      ),
    },
    {
      header: 'Outcome',
      cell: ({ row }) => (
        <Badge
          variant="light"
          color={row.original.status === 'PASSED' ? 'teal' : 'red'}
        >
          {row.original.status}
        </Badge>
      ),
    },
    {
      header: 'Appeal',
      cell: ({ row }) => {
        const appeal = deps.appeals.find((a) => a.resultId === row.original.id);
        return appeal ? (
          <Badge size="sm" variant="light" color="grape">
            {appeal.appealNumber} · {appeal.status}
          </Badge>
        ) : deps.can([PORTAL_PERMISSIONS.VIEW_OWN_EXAM]) ? (
          <Button
            size="compact-xs"
            variant="light"
            color="grape"
            leftSection={<IconGavel size={13} />}
            onClick={() => deps.onAppeal(row.original)}
          >
            Appeal
          </Button>
        ) : null;
      },
    },
  ];
}
