import {
  IconCircleArrowRight,
  IconClipboardCheck,
  IconClipboardX,
  IconFlag,
  IconRefresh,
  IconUser,
  IconWallpaper,
  type TablerIcon,
} from '@material-hu/icons/tabler';

import { type AvatarProps as HuAvatarProps } from '@material-hu/components/design-system/Avatar/types';

import {
  EventsType,
  ServiceItemTypes,
} from 'src/pages/dashboard/serviceManagement/types';

// Set of known/supported activity types that can be displayed in the UI
export const KNOWN_ACTIVITY_TYPES = new Set<EventsType>([
  EventsType.TASK_STARTED,
  EventsType.TASK_UPDATED,
  EventsType.TASK_ASSIGNED,
  EventsType.TASK_REASSIGNED,
  EventsType.APPROVAL_GROUP_APPROVED,
  EventsType.APPROVAL_GROUP_CREATED,
  EventsType.APPROVAL_GROUP_REJECTED,
  EventsType.APPROVAL_GROUP_CANCELLED,
  EventsType.APPROVAL_TASK_APPROVED,
  EventsType.APPROVAL_TASK_REJECTED,
  EventsType.APPROVAL_TASK_CANCELLED,
  EventsType.TASK_CLOSED_MANUALLY,
  EventsType.TASK_CLOSED_AUTOMATICALLY,
]);

// Checks if an activity type is known and supported for display
export const isKnownActivityType = (type: string): type is EventsType => {
  return KNOWN_ACTIVITY_TYPES.has(type as EventsType);
};

export const imageAccept = '.png, .gif, .jpeg, .webp';
export const fileAccept =
  '.pdf, .doc, .docx, .ppt, .pptx, .ppsx, .ppsm, .xls, .xlsx, .csv, .xlsm, .zip, .rar, .7z, .tar, .gz, .tgz, xz, .bz2, .avi, .mp4, .mov, .webm, .mpeg, .mkv, .xml';

export const MAX_ATTACHMENTS = 10;
export const MAX_ATTACHMENTS_SIZE = 100 * 1024 * 1024; // 100MB

export const IMG_GAP_IN_PX = 8;
export const MAX_CONTAINER_WIDTH = 586;
export const DEFAULT_FILE_CARD_WIDTH = '400px';

export const getActivityIcon = (
  type: EventsType,
  taskType: ServiceItemTypes,
): {
  Icon: TablerIcon;
  color: HuAvatarProps['color'];
} => {
  switch (type) {
    case EventsType.TASK_STARTED:
      if (taskType === ServiceItemTypes.SUBTASK) {
        return {
          Icon: IconWallpaper,
          color: 'primary',
        };
      }
      return {
        Icon: IconFlag,
        color: 'success',
      };

    // State change
    case EventsType.TASK_UPDATED:
      return {
        Icon: IconCircleArrowRight,
        color: taskType === ServiceItemTypes.SUBTASK ? 'primary' : 'info',
      };
    case EventsType.TASK_REASSIGNED:
      return {
        Icon: IconRefresh,
        color: taskType === ServiceItemTypes.SUBTASK ? 'primary' : 'highlight',
      };
    case EventsType.TASK_ASSIGNED:
      return {
        Icon: IconUser,
        color: taskType === ServiceItemTypes.SUBTASK ? 'primary' : 'highlight',
      };
    case EventsType.APPROVAL_GROUP_APPROVED:
      return {
        Icon: IconClipboardCheck,
        color: 'success',
      };
    case EventsType.APPROVAL_GROUP_REJECTED:
      return {
        Icon: IconClipboardX,
        color: 'error',
      };
    case EventsType.APPROVAL_TASK_APPROVED:
      return {
        Icon: IconClipboardCheck,
        color: 'success',
      };
    case EventsType.APPROVAL_TASK_CANCELLED:
    case EventsType.APPROVAL_GROUP_CANCELLED:
      return {
        Icon: IconClipboardX,
        color: undefined,
      };
    case EventsType.APPROVAL_GROUP_CREATED:
      return {
        Icon: IconClipboardCheck,
        color: 'highlight',
      };
    case EventsType.TASK_CLOSED_MANUALLY:
    case EventsType.TASK_CLOSED_AUTOMATICALLY:
      return {
        Icon: IconWallpaper,
        color: 'primary',
      };
    default:
      return {
        Icon: IconUser,
        color: 'highlight',
      };
  }
};

export const BIYI_COMU_ID = 142;
export const HUMAND_TESTING_ID = 11;
export const OXXO_ID = 5146;

export const OXXO_COLLABORATOR_TYPE_IDS = [
  BIYI_COMU_ID,
  HUMAND_TESTING_ID,
  OXXO_ID,
];

export const OXXO_SEGMENTATION_OPTIONS = {
  groupId: 6151,
  items: [
    {
      id: 140253,
      title: 'Operativo',
    },
    {
      id: 140259,
      title: 'Administrativo',
    },
    {
      id: 140265,
      title: 'Administrativo OS',
    },
    {
      id: 405330,
      title: 'Auditor de Inventarios',
    },
  ],
};

// Regex to match URLs starting with http://, https://, or www.
export const urlRegex = /(\b(?:https?:\/\/|www\.)[^\s]+)/gi;

export const UNAUTHORIZED_ERROR_CODE = 'PERMISSION_DENIED';
export const DUPLICATE_ENTITY_ERROR_CODE = 'DUPLICATE_ENTITY';

export const MIN_ZOOM_SCALE = 0.5;
export const MAX_ZOOM_SCALE = 2;

export const HEADER_HEIGHT = 73;
export const PDF_VIEWER_TOP_PADDING = 32;

// Shared between `agentWorkspace/Approvals.tsx` and
// `servicePortal/approvals/ApprovalsPortal.tsx`. Both screens use the same
// pending/history tabs and empty-state copy.
export const approvalTabs = {
  pending: 'pending',
  history: 'history',
} as const;

export type ApprovalTabsType = (typeof approvalTabs)[keyof typeof approvalTabs];

export const approvalNoSearchId = 'no-search';

export const approvalEmptyState = {
  [approvalTabs.pending]: {
    emptyTitle: 'approvals.empty.title',
    emptyDescription: 'approvals.empty.description',
  },
  [approvalTabs.history]: {
    emptyTitle: 'approvals.empty_history.title',
    emptyDescription: 'approvals.empty_history.description',
  },
  [approvalNoSearchId]: {
    emptyTitle: 'no_results_found_search',
    emptyDescription: 'try_again_search',
  },
};
