import { ActionIcon, Menu } from "@mantine/core";
import {
  IconDetails,
  IconDotsVertical,
  IconEdit,
  IconTrash,
  IconToggleRight,
} from "@tabler/icons-react";
import type { TFunction } from "i18next";
import type { AdvancedColumn } from "@ema-platform/ui";
import { LICENSE_PERMISSIONS, RequirePermission } from "@ema-platform/auth";
import type { Exam } from "../../types/exam";

export function examActionsColumn(
  t: TFunction,
  handlers: {
    onEdit: (exam: Exam) => void;
    onDelete: (exam: Exam) => void;
    onDetails: (exam: Exam) => void;
    onOpenStatusChange: (exam: Exam) => void;
    changingStatusId?: string | null;
  },
): AdvancedColumn<Exam> {
  return {
    header: t("exam.columns.actions", "Actions"),
    align: "right",
    cell: ({ row }) => (
      <Menu shadow="md" width={180} position="bottom-end">
        <Menu.Target>
          <ActionIcon
            variant="subtle"
            color="gray"
            size="sm"
            loading={handlers.changingStatusId === row.original.id}
          >
            <IconDotsVertical size={16} />
          </ActionIcon>
        </Menu.Target>
        <Menu.Dropdown>
          <Menu.Item
            leftSection={<IconDetails size={14} />}
            onClick={() => handlers.onDetails(row.original)}
          >
            {t("exam.action.details", "Details")}
          </Menu.Item>
          <RequirePermission anyOf={[LICENSE_PERMISSIONS.MANAGE_EXAMS]} hideOnly>
            <Menu.Item
              leftSection={<IconEdit size={14} />}
              onClick={() => handlers.onEdit(row.original)}
            >
              {t("exam.action.edit", "Edit")}
            </Menu.Item>
            <Menu.Item
              leftSection={<IconToggleRight size={14} />}
              onClick={() => handlers.onOpenStatusChange(row.original)}
            >
              {t("exam.form.status")}
            </Menu.Item>
            <Menu.Item
              color="red"
              leftSection={<IconTrash size={14} />}
              onClick={() => handlers.onDelete(row.original)}
            >
              {t("exam.action.delete", "Delete")}
            </Menu.Item>
          </RequirePermission>
        </Menu.Dropdown>
      </Menu>
    ),
  };
}
