import api from 'src/config/api';
import {
  type Category,
  CategoryOrderBy,
  type SegmentedParams,
  type ServiceItem,
  ServiceItemStatus,
} from 'src/pages/dashboard/serviceManagement/types';
import { type Pagination } from 'src/types/services';

export const getServiceItems = (
  page: number,
  query?: string,
  category?: Category,
  limit = 9,
) => {
  const params: {
    page: number;
    limit: number;
    name: string | null;
    role: string;
    status: ServiceItemStatus;
    categoryId?: string;
  } = {
    page,
    limit,
    name: query ? query : null,
    status: ServiceItemStatus.ACTIVE,
    role: 'USER',
  };
  if (category) {
    params.categoryId = category.id;
  }
  return api.get<Pagination<ServiceItem[]>>('workflows/catalog-items', {
    params,
  });
};

export const getSegmentedServiceItems = (params: SegmentedParams) => {
  const parsedParams = {
    page: params.page,
    limit: params.limit ?? 10,
    name: params.query || null,
    categoryId: params.categoryId || null,
    includeAssigned: params.includeAssigned ?? true,
    applyCandidateAgentRule: params.applyCandidateAgentRule ?? true,
    applyVisibilityRule: params.applyVisibilityRule ?? false,
    status: params.status ?? undefined,
    helpDeskId: params.helpDeskId ?? undefined,
  };

  return api.get<Pagination<ServiceItem[]>>(
    'workflows/catalog-items/segmented',
    {
      params: parsedParams,
    },
  );
};

export const getServiceItem = (id: string) =>
  api.get<ServiceItem>(`workflows/catalog-items/${id}`);

export const getCategories = (
  orderBy: CategoryOrderBy = CategoryOrderBy.NAME,
) =>
  api.get<Category[]>('workflows/categories/linked', { params: { orderBy } });
