import { identity, pickBy } from 'lodash-es';

import api from 'src/config/api';
import {
  type Agent,
  type AgentGroup,
  type ShortServiceManagementUser,
} from 'src/pages/dashboard/serviceManagement/types';
import { type Pagination } from 'src/types/services';

export const getAgent = (userId: number) =>
  api.get<Agent>(`workflows/agents/users/${userId}`);

export const getAgents = () => api.get<Agent[]>('workflows/agents');

export const getAgentCandidates = (
  serviceItemId: string,
  params: { search: string; page: number; limit?: number },
) => {
  return api.get<Pagination<ShortServiceManagementUser[]>>(
    `workflows/catalog-items/${serviceItemId}/candidate-agents`,
    {
      params: pickBy(
        {
          ...params,
        },
        identity,
      ),
    },
  );
};

export const assignAgentToTask = (taskId: string, agentId: number) =>
  api.patch(`workflows/tasks/${taskId}/assignee`, { assigneeId: agentId });

export const assignAgentToMultipleTasks = (
  taskIds: string[],
  agentId: number,
) =>
  api.patch('workflows/tasks/assignees', {
    assigneeId: agentId,
    taskIds,
  });

export const getAgentGroups = (params: {
  page: number;
  limit: number;
  name?: string;
  helpDeskId?: string;
}) =>
  api.get<Pagination<Omit<AgentGroup, 'agents'>[]>>(`workflows/agent-groups`, {
    params: pickBy(
      {
        ...params,
      },
      identity,
    ),
  });
