/**
 * @ai-summary Route definitions for backup API endpoints
 * @ai-context All routes require admin authentication via fastify.requireAdmin
 */

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { Pool } from 'pg';
import { BackupController } from './backup.controller';
import {
  ListBackupsQuery,
  CreateBackupBody,
  BackupIdParam,
  RestoreBody,
  CreateScheduleBody,
  UpdateScheduleBody,
  ScheduleIdParam,
  UpdateSettingsBody,
} from './backup.validation';

export async function registerBackupRoutes(
  fastify: FastifyInstance,
  opts: FastifyPluginOptions & { pool: Pool }
): Promise<void> {
  const controller = new BackupController(opts.pool);

  // ============================================
  // Backup Operations
  // ============================================

  // GET /api/admin/backups - List all backups
  fastify.get<{ Querystring: ListBackupsQuery }>('/admin/backups', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.listBackups.bind(controller),
  });

  // POST /api/admin/backups - Create manual backup
  fastify.post<{ Body: CreateBackupBody }>('/admin/backups', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.createBackup.bind(controller),
  });

  // GET /api/admin/backups/:id - Get backup details
  fastify.get<{ Params: BackupIdParam }>('/admin/backups/:id', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.getBackup.bind(controller),
  });

  // GET /api/admin/backups/:id/download - Download backup file
  fastify.get<{ Params: BackupIdParam }>('/admin/backups/:id/download', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.downloadBackup.bind(controller),
  });

  // DELETE /api/admin/backups/:id - Delete backup
  fastify.delete<{ Params: BackupIdParam }>('/admin/backups/:id', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.deleteBackup.bind(controller),
  });

  // POST /api/admin/backups/upload - Upload backup file
  fastify.post('/admin/backups/upload', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.uploadBackup.bind(controller),
  });

  // ============================================
  // Restore Operations
  // ============================================

  // POST /api/admin/backups/:id/restore/preview - Preview restore
  fastify.post<{ Params: BackupIdParam }>('/admin/backups/:id/restore/preview', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.previewRestore.bind(controller),
  });

  // POST /api/admin/backups/:id/restore - Execute restore
  fastify.post<{ Params: BackupIdParam; Body: RestoreBody }>('/admin/backups/:id/restore', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.executeRestore.bind(controller),
  });

  // GET /api/admin/backups/restore/status - Get restore status
  fastify.get('/admin/backups/restore/status', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.getRestoreStatus.bind(controller),
  });

  // ============================================
  // Schedule Operations
  // ============================================

  // GET /api/admin/backups/schedules - List schedules
  fastify.get('/admin/backups/schedules', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.listSchedules.bind(controller),
  });

  // POST /api/admin/backups/schedules - Create schedule
  fastify.post<{ Body: CreateScheduleBody }>('/admin/backups/schedules', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.createSchedule.bind(controller),
  });

  // PUT /api/admin/backups/schedules/:id - Update schedule
  fastify.put<{ Params: ScheduleIdParam; Body: UpdateScheduleBody }>('/admin/backups/schedules/:id', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.updateSchedule.bind(controller),
  });

  // DELETE /api/admin/backups/schedules/:id - Delete schedule
  fastify.delete<{ Params: ScheduleIdParam }>('/admin/backups/schedules/:id', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.deleteSchedule.bind(controller),
  });

  // PATCH /api/admin/backups/schedules/:id/toggle - Toggle schedule
  fastify.patch<{ Params: ScheduleIdParam }>('/admin/backups/schedules/:id/toggle', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.toggleSchedule.bind(controller),
  });

  // ============================================
  // Settings Operations
  // ============================================

  // GET /api/admin/backups/settings - Get settings
  fastify.get('/admin/backups/settings', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.getSettings.bind(controller),
  });

  // PUT /api/admin/backups/settings - Update settings
  fastify.put<{ Body: UpdateSettingsBody }>('/admin/backups/settings', {
    preHandler: [(fastify as any).requireAdmin],
    handler: controller.updateSettings.bind(controller),
  });
}
