import api from 'src/config/api';
import { type CommentFormValues } from 'src/pages/dashboard/serviceManagement/servicePortal/requestedDetails/components/Comments/utils';
import { type Event } from 'src/pages/dashboard/serviceManagement/types';

export const markLastReadComment = (taskId: string, commentId: string) =>
  api.post(`workflows/tasks/${taskId}/comments/${commentId}/read`);

export const sendComments = (
  taskId: string,
  commentData: CommentFormValues,
) => {
  const attachments: { id: number }[] = [];
  attachments.push(...commentData.images.map(image => ({ id: image.id })));
  attachments.push(...commentData.files.map(image => ({ id: image.id })));
  const body = {
    message: commentData.message || null,
    attachments,
  };
  return api.post<Event>(`workflows/tasks/${taskId}/comments`, body);
};

export const deleteComment = (taskId: string, commentId: string) =>
  api.delete(`workflows/tasks/${taskId}/comments/${commentId}`);
