import { ipcMain, BrowserWindow } from 'electron';
import log from 'electron-log';

export function registerBackupRestoreHandlers(): void {
  ipcMain.handle('backup-restore:get-containers', async () => {
    try {
      const { getCanvasPostgresContainers } = require('../services/backup-manager');
      return await getCanvasPostgresContainers();
    } catch (error) {
      log.error('Error getting containers:', error);
      return [];
    }
  });

  ipcMain.handle('backup-restore:get-backups', async () => {
    try {
      const { getBackups } = require('../services/backup-manager');
      return await getBackups();
    } catch (error) {
      log.error('Error getting backups:', error);
      return [];
    }
  });

  ipcMain.handle('backup-restore:set-default', async (_event, id: string, isDefault: boolean) => {
    try {
      const { setDefaultBackup } = require('../services/backup-manager');
      await setDefaultBackup(id, isDefault);
    } catch (error) {
      log.error('Error setting default backup:', error);
      throw error;
    }
  });

  ipcMain.handle('backup-restore:delete-backup', async (_event, id: string) => {
    try {
      const { deleteBackup } = require('../services/backup-manager');
      await deleteBackup(id);
    } catch (error) {
      log.error('Error deleting backup:', error);
      throw error;
    }
  });

  ipcMain.handle('backup-restore:create-backup', async (event, options: { containerName: string; notes: string }) => {
    try {
      const { createDatabaseBackup } = require('../services/backup-manager');
      const window = BrowserWindow.fromWebContents(event.sender);
      return await createDatabaseBackup(options.containerName, options.notes, window);
    } catch (error: any) {
      log.error('Error creating backup:', error);
      return { success: false, message: error.message };
    }
  });

  ipcMain.handle('backup-restore:restore-backup', async (event, options: { backupFilePath: string; containerName: string }) => {
    try {
      const { restoreDatabaseBackup } = require('../services/backup-manager');
      const window = BrowserWindow.fromWebContents(event.sender);
      return await restoreDatabaseBackup(options.backupFilePath, options.containerName, window);
    } catch (error: any) {
      log.error('Error restoring backup:', error);
      return { success: false, message: error.message };
    }
  });

  ipcMain.handle('backup-restore:select-backup-file', async (event) => {
    try {
      const { selectBackupFile } = require('../services/backup-manager');
      const window = BrowserWindow.fromWebContents(event.sender);
      return await selectBackupFile(window);
    } catch (error) {
      log.error('Error selecting backup file:', error);
      throw error;
    }
  });

  ipcMain.handle('backup-restore:export-backup', async (event, backupId: string) => {
    try {
      const { exportBackup } = require('../services/backup-manager');
      const window = BrowserWindow.fromWebContents(event.sender);
      return await exportBackup(backupId, window);
    } catch (error: any) {
      log.error('Error exporting backup:', error);
      return { success: false, message: error.message };
    }
  });

  ipcMain.handle('backup-restore:import-backup', async (_event, exportedZipPath: string) => {
    try {
      const { importBackup } = require('../services/backup-manager');
      return await importBackup(exportedZipPath);
    } catch (error: any) {
      log.error('Error importing backup:', error);
      return { success: false, message: error.message };
    }
  });

  ipcMain.handle('backup-restore:select-export-file', async (event) => {
    try {
      const { selectExportFile } = require('../services/backup-manager');
      const window = BrowserWindow.fromWebContents(event.sender);
      return await selectExportFile(window);
    } catch (error) {
      log.error('Error selecting export file:', error);
      throw error;
    }
  });

  log.info('Backup/Restore IPC handlers registered');
}
