import api from 'src/config/api';
import { type CategorizedHoursParams } from 'src/services/timeTracking';
import { type ApprovableStatus } from 'src/types/timeTracking';

export type CategorizedHoursListFilters = Omit<
  CategorizedHoursParams,
  'page' | 'limit' | 'sortBy' | 'sortOrder'
>;

export type CategorizedHourCategoryCount = {
  hourCategoryId: number;
  hourCategoryName: string;
  count: number;
};

export type GetCategorizedHoursCategoryCountsResponse = {
  hourCategoryCounts: CategorizedHourCategoryCount[];
  total: number;
};

export const getCategorizedHoursCategoryCounts = (
  filters: CategorizedHoursListFilters,
) =>
  api.get<GetCategorizedHoursCategoryCountsResponse>(
    '/time-tracking/categorized-hours/category-counts',
    { params: filters },
  );

export type BulkReviewCategorizedHoursParams = {
  state: ApprovableStatus;
  comment?: string;
} & (
  | {
      categorizedHourIds: string[];
      allRows?: never;
      filters?: never;
      excludeCategorizedHourIds?: never;
    }
  | {
      allRows: true;
      filters: CategorizedHoursListFilters;
      excludeCategorizedHourIds?: string[];
      categorizedHourIds?: never;
    }
);

export type BulkReviewCategorizedHoursResponse = {
  reviewedIds: string[];
  skippedCount: number;
};

export type BulkReviewCategorizedHoursError = {
  code: string;
  message: string;
  limit?: number;
};

export const bulkReviewCategorizedHours = (
  body: BulkReviewCategorizedHoursParams,
) =>
  api.post<BulkReviewCategorizedHoursResponse>(
    '/time-tracking/categorized-hours/bulk-review',
    body,
  );
