import { Controller, Get, Put, Body, Query } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { AlertsService } from './alerts.service';
import { UpdateAlertConfigDto } from './dto/update-alert-config.dto';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';

@ApiTags('alerts')
@ApiBearerAuth()
@Controller('alerts')
export class AlertsController {
  constructor(private readonly alertsService: AlertsService) {}

  @Get('config')
  @RequirePermission('alerts.view')
  @ApiOperation({ summary: 'Get alert configuration' })
  async getConfig(@CurrentTenant() licenseId: string) {
    return this.alertsService.getConfig(licenseId);
  }

  @Put('config')
  @RequirePermission('alerts.manage')
  @ApiOperation({ summary: 'Update alert configuration' })
  async updateConfig(
    @CurrentTenant() licenseId: string,
    @Body() dto: UpdateAlertConfigDto,
  ) {
    return this.alertsService.updateConfig(licenseId, dto);
  }

  @Get('history')
  @RequirePermission('alerts.view')
  @ApiOperation({ summary: 'Get alert history' })
  @ApiQuery({ name: 'limit', required: false, type: Number, description: 'Max records to return (default: 50)' })
  async getHistory(
    @CurrentTenant() licenseId: string,
    @Query('limit') limit?: string,
  ) {
    const limitNum = limit ? parseInt(limit, 10) : 50;
    return this.alertsService.getHistory(licenseId, limitNum);
  }
}
