import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, Roles, RolesGuard, UserRole } from '@genex/common';
import { TelemetryService } from '../../../application/services/telemetry.service';
import { QueryDauDto, QueryEventsDto } from '../dto/query-dau.dto';

@ApiTags('admin-telemetry')
@Controller('admin/telemetry')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminTelemetryController {
  constructor(
    private readonly telemetryService: TelemetryService,
  ) {}

  @Get('dau')
  @ApiOperation({ summary: 'Query DAU statistics' })
  async getDauStats(@Query() query: QueryDauDto) {
    const result = await this.telemetryService.getDauStats(query.startDate, query.endDate);
    return { code: 0, data: result };
  }

  @Get('events')
  @ApiOperation({ summary: 'Query telemetry events' })
  async listEvents(@Query() query: QueryEventsDto) {
    const page = query.page || 1;
    const limit = query.limit || 20;
    const result = await this.telemetryService.listEvents({
      eventName: query.eventName,
      userId: query.userId,
      page,
      limit,
    });
    return { code: 0, data: result };
  }

  @Get('realtime')
  @ApiOperation({ summary: 'Get realtime analytics dashboard data' })
  async getRealtimeData() {
    const data = await this.telemetryService.getRealtimeData();
    return { code: 0, data };
  }
}
