import { Controller, Get, Post, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { BackupService } from './backup.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';

@ApiTags('admin-backup')
@ApiBearerAuth()
@Controller('admin-api/backup')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
export class BackupController {
  constructor(private readonly backupService: BackupService) {}

  @Get('jobs')
  @ApiOperation({ summary: '备份任务列表' })
  @ApiQuery({ name: 'page', required: false })
  @ApiQuery({ name: 'limit', required: false })
  async listBackups(@Query('page') page?: string, @Query('limit') limit?: string) {
    return this.backupService.getBackupJobs(Number(page) || 1, Number(limit) || 20);
  }

  @Post('trigger/:type')
  @ApiOperation({ summary: '触发手动备份（mysql/qdrant/files）' })
  async triggerBackup(@Param('type') type: string) {
    return this.backupService.triggerBackup(type);
  }

  @Get('cleanup')
  @ApiOperation({ summary: '清理任务列表' })
  @ApiQuery({ name: 'page', required: false })
  @ApiQuery({ name: 'limit', required: false })
  async listCleanups(@Query('page') page?: string, @Query('limit') limit?: string) {
    return this.backupService.getCleanupJobs(Number(page) || 1, Number(limit) || 20);
  }

  @Post('cleanup/:type')
  @ApiOperation({ summary: '触发手动清理（soft-delete/api-metrics/task-logs）' })
  async triggerCleanup(@Param('type') type: string) {
    return this.backupService.triggerCleanup(type);
  }
}
