/**
 * API Client for Reporte de Vencimiento (synchronous CSV download)
 */
import { api } from '$lib/api';

export interface VencimientoFilter {
	days_ahead: number;
	currency: 'foreign' | 'national';
	client_id?: number | null;
	min_balance: number;
	conforme_anexo_31: boolean;
	usar_fecha_corte: boolean;
	fecha_corte?: string | null;
	send_email: boolean;
	julian_date: boolean;
}

export const vencimientoReportApi = {
	/** Generate and download CSV synchronously */
	generate: async (companyId: number, filters: VencimientoFilter): Promise<void> => {
		const blob = await api.postBlob(
			`/v1/a76/reports/movements/vencimiento/generate?company_id=${companyId}`,
			filters
		);
		const today = new Date().toISOString().slice(0, 10);
		const url = URL.createObjectURL(blob);
		const a = document.createElement('a');
		a.href = url;
		a.download = `vencimiento_${today}.csv`;
		document.body.appendChild(a);
		a.click();
		a.remove();
		URL.revokeObjectURL(url);
	}
};
