import {
  ApprovalStateName,
  CategoryOrderBy,
  ServiceItemTypes,
  TasksType,
} from 'src/pages/dashboard/serviceManagement/types';
import { type BubbleCode } from 'src/types/bubbaloo';

export const serviceManagementKeys = {
  configuration: () => ['configuration'],
  all: () => ['serviceManagement'],
  list: (query: string, categoryId: string) => [
    ...serviceManagementKeys.all(),
    query,
    categoryId,
  ],
  serviceItems: {
    all: () => [...serviceManagementKeys.all(), 'serviceItems'],
    list: (params: unknown) => [
      ...serviceManagementKeys.serviceItems.all(),
      params,
    ],
  },
  requester: {
    all: () => [...serviceManagementKeys.all(), 'requester'],
    list: (query: string) => [...serviceManagementKeys.requester.all(), query],
  },
  allDetail: () => [...serviceManagementKeys.all(), 'detail'],
  detail: (id: number | string) => [
    ...serviceManagementKeys.allDetail(),
    Number(id),
  ],
  form: {
    all: () => [...serviceManagementKeys.all(), 'form'],
    detail: (formTag: string) => [...serviceManagementKeys.form.all(), formTag],
    initialSectionForm: (tag: string) => [
      ...serviceManagementKeys.form.all(),
      tag,
      'initialSection',
    ],
    nextSectionForm: (tag: string, progressId: string) => [
      ...serviceManagementKeys.form.all(),
      tag,
      progressId,
      'formSection',
    ],
  },
  categories: {
    all: () => [...serviceManagementKeys.all(), 'categories'],
    orderedList: (orderBy: CategoryOrderBy) => [
      ...serviceManagementKeys.all(),
      orderBy,
    ],
    detail: (categoryId: string) => [
      ...serviceManagementKeys.categories.all(),
      categoryId,
    ],
  },
  tasks: {
    all: () => [...serviceManagementKeys.all(), 'tasks'],
    list: (params: unknown) => [...serviceManagementKeys.tasks.all(), params],
    detail: (taskId: string) => [...serviceManagementKeys.tasks.all(), taskId],
    hydratedList: (params: unknown) => [
      ...serviceManagementKeys.tasks.all(),
      'hydrated',
      params,
    ],
    count: (params: {
      role: TasksType;
      type: ServiceItemTypes;
      closed?: boolean | null;
    }) => [...serviceManagementKeys.tasks.all(), 'count', params],
    children: (taskId: string, params: unknown) => [
      ...serviceManagementKeys.tasks.all(),
      taskId,
      'children',
      params,
    ],
    childrenCount: (taskId: string, closed?: boolean | null) => [
      ...serviceManagementKeys.tasks.all(),
      taskId,
      'children',
      'count',
      closed,
    ],
  },
  comments: {
    all: () => [...serviceManagementKeys.all(), 'comments'],
    list: (taskId: string) => [...serviceManagementKeys.comments.all(), taskId],
  },
  activities: {
    all: () => [...serviceManagementKeys.all(), 'activities'],
    list: (taskId: string) => [
      ...serviceManagementKeys.activities.all(),
      taskId,
    ],
  },
  states: {
    all: (taskType?: ServiceItemTypes) => [
      ...serviceManagementKeys.all(),
      'states',
      taskType || ServiceItemTypes.INCIDENT,
    ],
    list: (stateId: string) => [...serviceManagementKeys.states.all(), stateId],
  },
  agents: {
    all: () => [...serviceManagementKeys.all(), 'agents'],
    list: (query: string) => [...serviceManagementKeys.agents.all(), query],
    candidates: (taskId: string, query: string) => [
      ...serviceManagementKeys.agents.all(),
      taskId,
      query,
    ],
    detail: (userId: number) => [...serviceManagementKeys.agents.all(), userId],
  },
  views: {
    all: () => [...serviceManagementKeys.all(), 'views'],
    list: () => [...serviceManagementKeys.views.all(), 'list'],
    detail: (viewId: string) => [...serviceManagementKeys.views.all(), viewId],
  },
  survey: {
    all: () => [...serviceManagementKeys.all(), 'survey'],
    template: (templateKey: string, taskId: string) => [
      ...serviceManagementKeys.survey.all(),
      templateKey,
      taskId,
    ],
  },
  agentGroups: {
    all: () => [...serviceManagementKeys.all(), 'agentGroups'],
    list: (params: unknown) => [
      ...serviceManagementKeys.agentGroups.all(),
      params,
    ],
  },
  helpDesk: {
    all: () => [...serviceManagementKeys.all(), 'helpDesk'],
    serviceMetrics: (helpDeskId: string, params: unknown) => [
      ...serviceManagementKeys.helpDesk.all(),
      helpDeskId,
      params,
      'serviceMetrics',
    ],
    associatedMembers: (helpDeskId: string, params: unknown) => [
      ...serviceManagementKeys.helpDesk.all(),
      helpDeskId,
      params,
      'associatedMembers',
    ],
  },
  bubbaloo: {
    all: () => [...serviceManagementKeys.all(), 'bubbaloo'],
    count: (bubbleCode: BubbleCode) => [
      ...serviceManagementKeys.bubbaloo.all(),
      bubbleCode,
    ],
  },
  approvalGroups: {
    all: () => [...serviceManagementKeys.all(), 'approvalGroups'],
    list: (taskId: string, params: unknown) => [
      ...serviceManagementKeys.approvalGroups.all(),
      taskId,
      params,
    ],
    group: (taskId: string, approvalGroupId: string) => [
      ...serviceManagementKeys.approvalGroups.all(),
      taskId,
      approvalGroupId,
    ],
    count: (taskId: string, statusIn?: ApprovalStateName[]) => [
      ...serviceManagementKeys.approvalGroups.all(),
      taskId,
      'count',
      statusIn,
    ],
  },
};
