import { apiFetch } from './api';

export async function getFactures(type?: string, managerId?: number) {
  let url = `/api/factures`;
  const params: string[] = [];
  if (type) params.push(`type=${type}`);
  if (managerId) params.push(`managerId=${managerId}`);
  if (params.length > 0) url += `?${params.join('&')}`;
  return apiFetch(url);
}

export async function getFacture(id: string) {
  return apiFetch(`/api/factures/${id}`);
}

export async function createFacture(data: FormData) {
  return apiFetch('/api/factures', { method: 'POST', body: data });
}

export async function updateFacture(id: string, data: FormData) {
  return apiFetch(`/api/factures/${id}`, { method: 'PUT', body: data });
}

export async function deleteFacture(id: string) {
  return apiFetch(`/api/factures/${id}`, { method: 'DELETE' });
}

export async function uploadFactureDocument(id: string, file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return apiFetch(`/api/factures/${id}/upload`, { method: 'POST', body: formData });
}

export function downloadFacture(id: string) {
  window.open(`/api/factures/${id}/download`, '_blank');
} 