import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { RaidableBasesService } from './raidablebases.service';
import { CreateRaidableBasesConfigDto } from './dto/create-raidablebases-config.dto';
import { UpdateRaidableBasesConfigDto } from './dto/update-raidablebases-config.dto';
import { ImportRaidableBasesConfigDto } from './dto/import-raidablebases-config.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('raidablebases')
@ApiBearerAuth()
@Controller('raidablebases')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class RaidableBasesController {
  constructor(private readonly raidableBasesService: RaidableBasesService) {}

  @Get('configs')
  @RequirePermission('raidablebases.view')
  @ApiOperation({ summary: 'List RaidableBases configs (summaries)' })
  getConfigs(@CurrentTenant() licenseId: string) {
    return this.raidableBasesService.getConfigs(licenseId);
  }

  @Get('configs/:id')
  @RequirePermission('raidablebases.view')
  @ApiOperation({ summary: 'Get full RaidableBases config with data' })
  getConfig(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.raidableBasesService.getConfig(licenseId, id);
  }

  @Post('configs')
  @RequirePermission('raidablebases.manage')
  @ApiOperation({ summary: 'Create RaidableBases config' })
  createConfig(@CurrentTenant() licenseId: string, @Body() dto: CreateRaidableBasesConfigDto) {
    return this.raidableBasesService.createConfig(licenseId, dto);
  }

  @Put('configs/:id')
  @RequirePermission('raidablebases.manage')
  @ApiOperation({ summary: 'Update RaidableBases config' })
  updateConfig(
    @CurrentTenant() licenseId: string,
    @Param('id') id: string,
    @Body() dto: UpdateRaidableBasesConfigDto,
  ) {
    return this.raidableBasesService.updateConfig(licenseId, id, dto);
  }

  @Delete('configs/:id')
  @RequirePermission('raidablebases.manage')
  @ApiOperation({ summary: 'Delete RaidableBases config' })
  deleteConfig(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.raidableBasesService.deleteConfig(licenseId, id);
  }

  @Post('configs/:id/apply')
  @RequirePermission('raidablebases.manage')
  @ApiOperation({ summary: 'Deploy RaidableBases config to server' })
  applyToServer(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.raidableBasesService.applyToServer(licenseId, id);
  }

  @Post('import-from-server')
  @RequirePermission('raidablebases.manage')
  @ApiOperation({ summary: 'Import RaidableBases.json from server via NATS' })
  importFromServer(@CurrentTenant() licenseId: string, @Body() dto: ImportRaidableBasesConfigDto) {
    return this.raidableBasesService.importFromServer(licenseId, dto.config_name, dto.description);
  }
}
