/**
 * Service layer for CreateRequest API operations
 */

import {
  createWorkflowMultipart,
  updateWorkflow,
  updateWorkflowMultipart,
} from '@/services/workflowApi';
import {
  CreateWorkflowPayload,
  UpdateWorkflowPayload,
} from '../types/createRequest.types';

/**
 * Create a new workflow (supports both draft and direct submission via isDraft flag)
 */
export async function createWorkflow(
  payload: CreateWorkflowPayload,
  documents: File[]
): Promise<{ id: string }> {
  const res = await createWorkflowMultipart(
    payload as any,
    documents || [],
    'SUPPORTING'
  );
  return { id: (res as any).id };
}

/**
 * Update an existing workflow (supports both draft and direct submission via isDraft flag)
 */
export async function updateWorkflowRequest(
  requestId: string,
  payload: UpdateWorkflowPayload,
  documents: File[],
  documentsToDelete: string[]
): Promise<void> {
  const hasNewFiles = documents && documents.length > 0;
  const hasDeletions = documentsToDelete.length > 0;

  if (hasNewFiles || hasDeletions) {
    await updateWorkflowMultipart(
      requestId,
      payload,
      documents || [],
      documentsToDelete
    );
  } else {
    await updateWorkflow(requestId, payload);
  }
}
