import { apiGet, apiPost, apiPut } from './client';
import type { SettingsFormData } from '@/types';

// Obtener configuración actual
export const getSettings = async () => {
  return apiGet('/api/settings');
};

// Actualizar configuración
export const updateSettings = async (data: SettingsFormData) => {
  return apiPut('/api/settings', data);
};

// Probar conexión con IGDB
export const testIgdbConnection = async (apiKey: string) => {
  return apiPost('/api/settings/test-igdb', { apiKey });
};

// Probar conexión con RAWG
export const testRawgConnection = async (apiKey: string) => {
  return apiPost('/api/settings/test-rawg', { apiKey });
};

// Probar conexión con TheGamesDB
export const testThegamesdbConnection = async (apiKey: string) => {
  return apiPost('/api/settings/test-thegamesdb', { apiKey });
};

// Obtener estado de servicios externos
export const getExternalServicesStatus = async () => {
  return apiGet<{
    igdb: { connected: boolean; lastChecked?: string };
    rawg: { connected: boolean; lastChecked?: string };
    thegamesdb: { connected: boolean; lastChecked?: string };
  }>('/api/settings/services-status');
};

// Obtener configuración de importación automática
export const getAutoImportConfig = async () => {
  return apiGet('/api/settings/auto-import');
};

// Actualizar configuración de importación automática
export const updateAutoImportConfig = async (enabled: boolean, directory?: string) => {
  return apiPut('/api/settings/auto-import', { enabled, directory });
};

// Obtener configuración de exportación
export const getExportConfig = async () => {
  return apiGet('/api/settings/export');
};

// Actualizar configuración de exportación
export const updateExportConfig = async (format: 'csv' | 'json', fields: string[]) => {
  return apiPut('/api/settings/export', { format, fields });
};

// Exportar datos
export const exportData = async (format: 'csv' | 'json', filters?: Record<string, any>) => {
  const params = new URLSearchParams();
  params.append('format', format);

  if (filters) {
    Object.entries(filters).forEach(([key, value]) => {
      if (value !== undefined && value !== null) {
        params.append(key, String(value));
      }
    });
  }

  const response = await fetch(`/api/settings/export?${params.toString()}`, {
    method: 'GET',
  });

  if (!response.ok) {
    throw new Error('Error al exportar datos');
  }

  return response.blob();
};

// Obtener estadísticas del sistema
export const getSystemStats = async () => {
  return apiGet<{
    totalGames: number;
    totalPlatforms: number;
    totalTags: number;
    totalRoms: number;
    totalSize: number;
    averageRating: number;
    recentActivity: Array<{
      type: string;
      message: string;
      timestamp: string;
    }>;
  }>('/api/settings/system-stats');
};

// Limpiar caché
export const clearCache = async () => {
  return apiPost('/api/settings/clear-cache');
};

// Obtener versión del sistema
export const getSystemVersion = async () => {
  return apiGet('/api/settings/version');
};
