export interface Client {
  uuid: string;
  hardware_token?: string;
  ip?: string;
  type?: string;
  hostname?: string;
  os_version?: string;
  software_version?: string;
  macs?: string;
  model?: string;
  description?: string;
  registration_time?: string;
  last_alive?: string;
  is_active?: boolean;
  group_id?: number;
  // Für Health-Status
  is_alive?: boolean;
}

export interface Group {
  id: number;
  name: string;
  created_at?: string;
  is_active?: boolean;
  clients: Client[];
}
// Liefert alle Gruppen mit zugehörigen Clients
export async function fetchGroupsWithClients(): Promise<Group[]> {
  const response = await fetch('/api/groups/with_clients');
  if (!response.ok) {
    throw new Error('Fehler beim Laden der Gruppen mit Clients');
  }
  return await response.json();
}

export async function fetchClients(): Promise<Client[]> {
  const response = await fetch('/api/clients');
  if (!response.ok) {
    throw new Error('Fehler beim Laden der Clients');
  }
  return await response.json();
}

export async function fetchClientsWithoutDescription(): Promise<Client[]> {
  const response = await fetch('/api/clients/without_description');
  if (!response.ok) {
    throw new Error('Fehler beim Laden der Clients ohne Beschreibung');
  }
  return await response.json();
}

export async function setClientDescription(uuid: string, description: string) {
  const res = await fetch(`/api/clients/${uuid}/description`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ description }),
  });
  if (!res.ok) throw new Error('Fehler beim Setzen der Beschreibung');
  return await res.json();
}

export async function updateClientGroup(clientIds: string[], groupId: number) {
  const res = await fetch('/api/clients/group', {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ client_ids: clientIds, group_id: groupId }),
  });
  if (!res.ok) throw new Error('Fehler beim Aktualisieren der Clients');
  return await res.json();
}

export async function updateClient(uuid: string, data: { description?: string; model?: string }) {
  const res = await fetch(`/api/clients/${uuid}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data),
  });
  if (!res.ok) throw new Error('Fehler beim Aktualisieren des Clients');
  return await res.json();
}

export async function restartClient(uuid: string): Promise<{ success: boolean; message?: string }> {
  const response = await fetch(`/api/clients/${uuid}/restart`, {
    method: 'POST',
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Fehler beim Neustart des Clients');
  }
  return await response.json();
}

export async function deleteClient(uuid: string) {
  const res = await fetch(`/api/clients/${uuid}`, {
    method: 'DELETE',
  });
  if (!res.ok) throw new Error('Fehler beim Entfernen des Clients');
  return await res.json();
}

export async function fetchMediaById(mediaId: number | string) {
  const response = await fetch(`/api/eventmedia/${mediaId}`);
  if (!response.ok) throw new Error('Fehler beim Laden der Mediainformationen');
  return await response.json();
}
