import { apiClient } from './client';
import type {
  LabCaseComment,
  LabTaskListItem,
  LabTaskStatus,
  ListLabTasksParams,
  LocateTaskPageParams,
  LocateTaskPageResult,
  PaginatedLabTasks,
  TaskFilterOptions,
} from '@/types/cases';

export const tasksApi = {
  list: async (
    params: ListLabTasksParams = {},
  ): Promise<{ success: boolean; data: PaginatedLabTasks }> => {
    const response = await apiClient.get('/tasks', { params });
    return response.data;
  },

  filterOptions: async (): Promise<{ success: boolean; data: TaskFilterOptions }> => {
    const response = await apiClient.get('/tasks/filter-options');
    return response.data;
  },

  locatePage: async (
    params: LocateTaskPageParams,
  ): Promise<{ success: boolean; data: LocateTaskPageResult }> => {
    const response = await apiClient.get('/tasks/locate-page', { params });
    return response.data;
  },

  updateStatus: async (
    taskId: string,
    status: LabTaskStatus,
  ): Promise<{ success: boolean; data: LabTaskListItem }> => {
    const response = await apiClient.patch(`/tasks/${taskId}`, { status });
    return response.data;
  },

  listComments: async (
    caseId: string,
  ): Promise<{ success: boolean; data: LabCaseComment[] }> => {
    const response = await apiClient.get(`/case-comments/${caseId}`);
    return response.data;
  },

  addComment: async (
    caseId: string,
    payload: { body: string; visibleToClinic?: boolean },
  ): Promise<{ success: boolean; data: LabCaseComment }> => {
    const response = await apiClient.post(`/case-comments/${caseId}`, payload);
    return response.data;
  },

  setCommentVisibility: async (
    commentId: string,
    visibleToClinic: boolean,
  ): Promise<{ success: boolean; data: LabCaseComment }> => {
    const response = await apiClient.patch(`/case-comments/item/${commentId}/visibility`, {
      visibleToClinic,
    });
    return response.data;
  },
};
