import {
  Controller,
  Post,
  Get,
  Put,
  Delete,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { NotificationService } from './notification.service';
import { TemplateService } from './services/template.service';
import {
  SendNotificationDto,
  SendBatchDto,
  CreateTemplateDto,
  UpdateTemplateDto,
  TestRenderDto,
  QueryLogsDto,
} from './dto/notification.dto';
import { RequirePermissions } from '@common/decorators/permissions.decorator';
import { Auditable, Sensitive, Financial } from '@core/observability/audit/decorators/auditable.decorator';

@Controller('notifications')
@RequirePermissions('notification:read')
export class NotificationController {
  constructor(
    private readonly notificationService: NotificationService,
    private readonly templateService: TemplateService,
  ) {}

  @Post('send')
  @Auditable()
  @RequirePermissions('notification:create')
  async send(@Body() dto: SendNotificationDto) {
    return this.notificationService.send(dto);
  }

  @Post('batch')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:create')
  async sendBatch(@Body() dto: SendBatchDto) {
    return this.notificationService.sendBatch(dto.notifications);
  }

  @Get('logs')
  @RequirePermissions('notification:read')
  async getLogs(@Query() query: QueryLogsDto) {
    return this.notificationService.getLogs({
      recipientId: query.recipientId,
      channel: query.channel,
      status: query.status as any,
      startDate: query.startDate ? new Date(query.startDate) : undefined,
      endDate: query.endDate ? new Date(query.endDate) : undefined,
      page: query.page,
      limit: query.limit,
    });
  }

  @Get('my-logs')
  async getMyLogs(
    @Query('page') page = 1,
    @Query('limit') limit = 50,
  ) {
    return { message: 'Use GET /logs?recipientId=xxx instead' };
  }

  @Post(':id/retry')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:admin')
  async retry(@Param('id') id: string) {
    return this.notificationService.retryFailed(id);
  }

  @Get('statistics')
  @RequirePermissions('notification:read')
  async getStatistics(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    return this.notificationService.getStatistics(
      startDate ? new Date(startDate) : undefined,
      endDate ? new Date(endDate) : undefined,
    );
  }

  @Get('templates')
  async getTemplates(@Query('channel') channel?: string) {
    return this.templateService.findAll(channel);
  }

  @Get('templates/:code')
  async getTemplate(@Param('code') code: string) {
    return this.templateService.findByCode(code);
  }

  @Post('templates')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:admin')
  async createTemplate(@Body() dto: CreateTemplateDto) {
    return this.templateService.create(dto as any);
  }

  @Put('templates/:code')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:admin')
  async updateTemplate(
    @Param('code') code: string,
    @Body() dto: UpdateTemplateDto,
  ) {
    return this.templateService.update(code, dto);
  }

  @Delete('templates/:code')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:admin')
  async deleteTemplate(@Param('code') code: string) {
    return this.templateService.delete(code);
  }

  @Post('templates/test-render')
  @Auditable()
  @Sensitive()
  @RequirePermissions('notification:admin')
  async testRender(@Body() dto: TestRenderDto) {
    return this.templateService.testRender(dto.templateCode, dto.variables);
  }
}
