import { apiClient } from './client';
import type { AlertaFull, AlertaCreate, AlertaUpdate, AlertasStats } from '@horux/shared';

export async function getAlertas(filters?: { leida?: boolean; resuelta?: boolean; contribuyenteId?: string }): Promise<AlertaFull[]> {
  const params = new URLSearchParams();
  if (filters?.leida !== undefined) params.set('leida', String(filters.leida));
  if (filters?.resuelta !== undefined) params.set('resuelta', String(filters.resuelta));
  if (filters?.contribuyenteId) params.set('contribuyenteId', filters.contribuyenteId);
  const response = await apiClient.get<AlertaFull[]>(`/alertas?${params}`);
  return response.data;
}

export async function getAlertasAutomaticas(contribuyenteId?: string): Promise<any[]> {
  const params = contribuyenteId ? `?contribuyenteId=${encodeURIComponent(contribuyenteId)}` : '';
  const response = await apiClient.get<any[]>(`/alertas/automaticas${params}`);
  return response.data;
}

export async function getStats(): Promise<AlertasStats> {
  const response = await apiClient.get<AlertasStats>('/alertas/stats');
  return response.data;
}

export async function createAlerta(data: AlertaCreate): Promise<AlertaFull> {
  const response = await apiClient.post<AlertaFull>('/alertas', data);
  return response.data;
}

export async function updateAlerta(id: number, data: AlertaUpdate): Promise<AlertaFull> {
  const response = await apiClient.patch<AlertaFull>(`/alertas/${id}`, data);
  return response.data;
}

export async function deleteAlerta(id: number): Promise<void> {
  await apiClient.delete(`/alertas/${id}`);
}

export async function markAllAsRead(): Promise<void> {
  await apiClient.post('/alertas/mark-all-read');
}
