import { MQTTMessage, TopicNode } from '../types';

export async function getTopics(): Promise<TopicNode> {
  const res = await fetch('/api/topics');
  if (!res.ok) throw new Error('Impossible de charger les topics');
  return res.json();
}

export async function getHistory(topic: string, limit = 50): Promise<MQTTMessage[]> {
  const res = await fetch(`/api/topic/${encodeURIComponent(topic)}/history?limit=${limit}`);
  if (!res.ok) throw new Error('Impossible de charger l\'historique');
  return res.json();
}

export async function getStats(): Promise<{ count: number; size: string }> {
  const res = await fetch('/api/stats');
  if (!res.ok) throw new Error('Impossible de charger les stats');
  return res.json();
}

export async function getMetrics(): Promise<{
  cpuPercent: number;
  memBytes: number;
  memLimit: number;
  dbBytes: number;
  dbSize: string;
}> {
  const res = await fetch('/api/metrics');
  if (!res.ok) throw new Error('Impossible de charger les métriques');
  return res.json();
}

export async function getSysinfo(): Promise<{
  version: string;
  clients: string;
  msgReceived: string;
  msgSent: string;
  msgStored: string;
  subscriptions: string;
}> {
  const res = await fetch('/api/sysinfo');
  if (!res.ok) throw new Error('Impossible de charger SYS');
  return res.json();
}

export async function setFilters(rules: { topic: string; save: boolean; view: boolean }[]): Promise<void> {
  const res = await fetch('/api/filters', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(rules)
  });
  if (!res.ok) throw new Error('Impossible de mettre à jour les filtres');
}

export async function getSettings(): Promise<any> {
  const res = await fetch('/api/settings');
  if (!res.ok) throw new Error('Impossible de charger les paramètres');
  return res.json();
}

export async function saveSettings(payload: any): Promise<void> {
  const res = await fetch('/api/settings', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload)
  });
  if (!res.ok) throw new Error('Impossible de sauvegarder les paramètres');
}

export async function clearHistory(topic: string): Promise<{ deleted: number }> {
  const res = await fetch(`/api/topic/${encodeURIComponent(topic)}/clear-history`, {
    method: 'POST'
  });
  if (!res.ok) throw new Error('Impossible de supprimer l\'historique');
  return res.json();
}

export async function clearAllHistory(): Promise<{ deleted: number }> {
  const res = await fetch('/api/history/clear', {
    method: 'POST'
  });
  if (!res.ok) throw new Error('Impossible de supprimer la base');
  return res.json();
}

export async function publishMessage(payload: {
  topic: string;
  payload: string;
  qos: number;
  retained: boolean;
}): Promise<void> {
  const res = await fetch('/api/publish', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload)
  });
  if (!res.ok) throw new Error('Publication impossible');
}

export async function testConnection(broker: string): Promise<{ ok: boolean; latency?: number; error?: string }> {
  const res = await fetch('/api/test-connection', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ broker })
  });
  if (!res.ok) throw new Error('Test impossible');
  return res.json();
}
