import api from './api';

export interface CategorySales {
  categoryId: number;
  categoryName: string;
  totalSales: number;
  adCount: number;
  percentage: number;
}

export const reportService = {
  getSalesByCategory: async (from?: string, to?: string): Promise<CategorySales[]> => {
    const response = await api.get<CategorySales[]>('/reports/sales-by-category', {
      params: { from, to }
    });
    return response.data;
  },
  exportCierre: async (from: string, to: string, userId?: number) => {
    const response = await api.get(`/reports/export-cierre`, {
      params: { from, to, userId },
      responseType: 'blob'
    });

    const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', userId ? `Caja_Cajero_${userId}.pdf` : `Cierre_Global.pdf`);
    document.body.appendChild(link);
    link.click();
    link.remove();
  }
};