import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { WipesService } from './wipes.service';
import { CreateProfileDto } from './dto/create-profile.dto';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { CreateScheduleDto } from './dto/create-schedule.dto';
import { TriggerWipeDto } from './dto/trigger-wipe.dto';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';

@ApiTags('wipes')
@ApiBearerAuth()
@Controller('wipes')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class WipesController {
  constructor(private readonly wipesService: WipesService) {}

  @Get('profiles')
  @RequirePermission('wipe.view')
  @ApiOperation({ summary: 'Get all wipe profiles for tenant' })
  getProfiles(@CurrentTenant() licenseId: string) {
    return this.wipesService.getProfiles(licenseId);
  }

  @Post('profiles')
  @RequirePermission('wipe.manage')
  @ApiOperation({ summary: 'Create new wipe profile' })
  createProfile(@CurrentTenant() licenseId: string, @Body() dto: CreateProfileDto) {
    return this.wipesService.createProfile(licenseId, dto);
  }

  @Put('profiles/:id')
  @RequirePermission('wipe.manage')
  @ApiOperation({ summary: 'Update wipe profile' })
  updateProfile(
    @CurrentTenant() licenseId: string,
    @Param('id') profileId: string,
    @Body() dto: UpdateProfileDto,
  ) {
    return this.wipesService.updateProfile(licenseId, profileId, dto);
  }

  @Delete('profiles/:id')
  @RequirePermission('wipe.manage')
  @ApiOperation({ summary: 'Delete wipe profile' })
  async deleteProfile(@CurrentTenant() licenseId: string, @Param('id') profileId: string) {
    await this.wipesService.deleteProfile(licenseId, profileId);
    return { deleted: true };
  }

  @Get('schedules')
  @RequirePermission('wipe.view')
  @ApiOperation({ summary: 'Get all wipe schedules for tenant' })
  getSchedules(@CurrentTenant() licenseId: string) {
    return this.wipesService.getSchedules(licenseId);
  }

  @Post('schedules')
  @RequirePermission('wipe.manage')
  @ApiOperation({ summary: 'Create new wipe schedule' })
  createSchedule(@CurrentTenant() licenseId: string, @Body() dto: CreateScheduleDto) {
    return this.wipesService.createSchedule(licenseId, dto);
  }

  @Get('history')
  @RequirePermission('wipe.view')
  @ApiOperation({ summary: 'Get wipe history for tenant' })
  @ApiQuery({ name: 'limit', required: false, example: 50 })
  getHistory(
    @CurrentTenant() licenseId: string,
    @Query('limit') limit?: string,
  ) {
    const limitNum = limit ? parseInt(limit, 10) : 50;
    return this.wipesService.getHistory(licenseId, limitNum);
  }

  @Post('trigger')
  @RequirePermission('wipe.execute')
  @ApiOperation({ summary: 'Trigger manual wipe' })
  triggerWipe(@CurrentTenant() licenseId: string, @Body() dto: TriggerWipeDto) {
    return this.wipesService.triggerWipe(licenseId, dto);
  }

  @Post('dry-run')
  @RequirePermission('wipe.execute')
  @ApiOperation({ summary: 'Simulate wipe and return what would be affected' })
  triggerDryRun(@CurrentTenant() licenseId: string, @Body() dto: TriggerWipeDto) {
    return this.wipesService.triggerDryRun(licenseId, dto);
  }
}
