const express = require('express');

module.exports = function(ctx) {
  const router = express.Router();

  // Get backup configuration
  router.get('/backups/config', ctx.asyncHandler(async (req, res) => {
    const config = ctx.backupManager.getConfig();
    res.json({ success: true, config });
  }, 'backups-config-get'));

  // Update backup configuration
  router.post('/backups/config', ctx.asyncHandler(async (req, res) => {
    ctx.backupManager.updateConfig(req.body);
    res.json({ success: true, message: 'Backup configuration updated' });
  }, 'backups-config-update'));

  // Execute manual backup
  router.post('/backups/execute', ctx.asyncHandler(async (req, res) => {
    const backup = await ctx.backupManager.executeBackup('manual', req.body);
    res.json({ success: true, backup });
  }, 'backups-execute'));

  // Get backup history
  router.get('/backups/history', ctx.asyncHandler(async (req, res) => {
    const limit = parseInt(req.query.limit) || 50;
    const history = ctx.backupManager.getHistory(limit);
    res.json({ success: true, history });
  }, 'backups-history'));

  // Restore from backup
  router.post('/backups/restore/:backupId', ctx.asyncHandler(async (req, res) => {
    const result = await ctx.backupManager.restoreBackup(req.params.backupId, req.body);
    res.json({ success: true, result });
  }, 'backups-restore'));

  return router;
};
