import { ActionIcon, Menu } from '@mantine/core';
import { IconDotsVertical, IconEye, IconSend, IconTrash } 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 { Result } from '../../types/result';

export type QcAction = 'moderate' | 'approve' | 'return';

export function resultActionsColumn(
  t: TFunction,
  handlers: {
    onQc: (result: Result, action: QcAction) => void;
    onViewDetail: (result: Result) => void;
    onDelete: (result: Result) => void;
    onPublish: (result: Result) => void;
  },
): AdvancedColumn<Result> {
  return {
    header: '',
    label: t('result.columns.actions', 'Actions'),
    cell: ({ row }) => {
      const r = row.original;
      return (
        <Menu shadow="md" width={180} position="bottom-end">
          <Menu.Target>
            <ActionIcon variant="subtle" color="gray" size="sm">
              <IconDotsVertical size={16} />
            </ActionIcon>
          </Menu.Target>
          <Menu.Dropdown>
            <Menu.Item leftSection={<IconEye size={14} />} onClick={() => handlers.onViewDetail(r)}>
              {t('result.action.viewEdit')}
            </Menu.Item>
            {(r.reviewStatus === 'MARKED' || r.reviewStatus === 'MODERATED') && (
              <>
                <RequirePermission anyOf={[LICENSE_PERMISSIONS.MODERATE_EXAM_RESULT]} hideOnly>
                  <Menu.Item color="yellow" onClick={() => handlers.onQc(r, 'moderate')}>
                    {t('result.review.moderate')}
                  </Menu.Item>
                </RequirePermission>
                <RequirePermission anyOf={[LICENSE_PERMISSIONS.APPROVE_EXAM_RESULT]} hideOnly>
                  <Menu.Item color="blue" onClick={() => handlers.onQc(r, 'approve')}>
                    {t('result.review.approve')}
                  </Menu.Item>
                </RequirePermission>
              </>
            )}
            {(r.reviewStatus === 'APPROVED' || r.reviewStatus === 'MODERATED') && (
              <RequirePermission
                anyOf={[
                  LICENSE_PERMISSIONS.MODERATE_EXAM_RESULT,
                  LICENSE_PERMISSIONS.APPROVE_EXAM_RESULT,
                ]}
                hideOnly
              >
                <Menu.Item color="orange" onClick={() => handlers.onQc(r, 'return')}>
                  {t('result.review.return')}
                </Menu.Item>
              </RequirePermission>
            )}
            {r.reviewStatus === 'APPROVED' && (
              <RequirePermission anyOf={[LICENSE_PERMISSIONS.PUBLISH_EXAM_RESULT]} hideOnly>
                <Menu.Item
                  color="teal"
                  leftSection={<IconSend size={14} />}
                  onClick={() => handlers.onPublish(r)}
                >
                  {t('result.review.publish')}
                </Menu.Item>
              </RequirePermission>
            )}
            <RequirePermission anyOf={[LICENSE_PERMISSIONS.APPROVE_EXAM_RESULT]} hideOnly>
              <Menu.Divider />
              <Menu.Item
                color="red"
                leftSection={<IconTrash size={14} />}
                onClick={() => handlers.onDelete(r)}
              >
                {t('result.action.delete')}
              </Menu.Item>
            </RequirePermission>
          </Menu.Dropdown>
        </Menu>
      );
    },
  };
}
