import { Controller, Post, Get, Body, Query, UseGuards } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { BatchEventsDto } from '../dto/request/batch-events.dto';
import { QueryDauDto } from '../dto/request/query-dau.dto';
import { DauStatsResponseDto } from '../dto/response/dau-stats.dto';
import { RecordEventsCommand } from '../../application/commands/record-events/record-events.command';
import { GetDauStatsQuery } from '../../application/queries/get-dau-stats/get-dau-stats.query';
import { Public } from '../../shared/decorators/public.decorator';
import { AdminGuard } from '../../shared/guards/admin.guard';

@ApiTags('Analytics')
@Controller('analytics')
export class AnalyticsController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
  ) {}

  @Post('events')
  @Public()
  @ApiOperation({ summary: '批量上报事件' })
  async batchEvents(@Body() dto: BatchEventsDto) {
    return this.commandBus.execute(new RecordEventsCommand(dto.events));
  }

  @Get('dau')
  @UseGuards(AdminGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '查询DAU统计（管理员 JWT）' })
  async getDauStats(@Query() dto: QueryDauDto): Promise<DauStatsResponseDto> {
    return this.queryBus.execute(
      new GetDauStatsQuery(new Date(dto.startDate), new Date(dto.endDate)),
    );
  }
}
