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

// Iniciar escaneo de directorio
export const startDirectoryScan = async (directoryPath: string) => {
  return apiPost('/api/import/scan', { directoryPath });
};

// Obtener estado del escaneo
export const getScanStatus = async (scanId: string) => {
  return apiGet(`/api/import/scan/${scanId}/status`);
};

// Obtener lista de ROMs detectadas
export const getDetectedRoms = async (scanId: string) => {
  return apiGet(`/api/import/scan/${scanId}/roms`);
};

// Procesar ROMs detectadas
export const processDetectedRoms = async (scanId: string, romIds: number[]) => {
  return apiPost(`/api/import/scan/${scanId}/process`, { romIds });
};

// Importar ROM manualmente
export const importRom = async (data: ImportRomFormData & { file: File }) => {
  const { file, ...rest } = data;
  return apiUpload('/api/import/rom', file, rest);
};

// Obtener historial de importaciones
export const getImportHistory = async (page: number = 1, limit: number = 10) => {
  return apiGet(`/api/import/history?page=${page}&limit=${limit}`);
};

// Obtener estadísticas de importación
export const getImportStats = async () => {
  return apiGet<{
    totalImports: number;
    successfulImports: number;
    failedImports: number;
    totalRomsScanned: number;
    totalRomsImported: number;
    averageProcessingTime: number;
    lastImportDate?: string;
  }>('/api/import/stats');
};

// Cancelar escaneo en progreso
export const cancelScan = async (scanId: string) => {
  return apiDelete(`/api/import/scan/${scanId}`);
};

// Reintentar importación fallida
export const retryImport = async (importId: number) => {
  return apiPost(`/api/import/retry/${importId}`);
};

// Eliminar registro de importación
export const deleteImportRecord = async (importId: number) => {
  return apiDelete(`/api/import/history/${importId}`);
};

// Obtener información de archivo ROM
export const getRomInfo = async (romPath: string) => {
  return apiGet(`/api/import/rom-info?path=${encodeURIComponent(romPath)}`);
};

// Verificar integridad de ROM
export const verifyRomIntegrity = async (romPath: string, romType: string) => {
  return apiPost('/api/import/verify-rom', { romPath, romType });
};

// Obtener directorios disponibles para escaneo
export const getAvailableDirectories = async () => {
  return apiGet<string[]>('/api/import/directories');
};
