import { Controller, Get, Query } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { DashboardService } from '../../application/services/dashboard.service';

@ApiTags('Reports')
@ApiBearerAuth()
@Controller('reports')
export class ReportsController {
  constructor(private readonly dashboardService: DashboardService) {}

  @Get('daily')
  @ApiOperation({ summary: '获取每日报表' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiQuery({ name: 'days', required: false, type: Number })
  async getDailyReports(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('days') days?: number,
  ) {
    const result = await this.dashboardService.getReports(
      page ?? 1,
      pageSize ?? 30,
    );

    // 转换为前端期望的格式
    return {
      items: result.data.map((report: any) => ({
        id: report.id,
        reportDate: report.reportDate,
        totalUsers: report.users?.total || 0,
        newUsers: report.users?.new || 0,
        adoptedUsers: report.adoptions?.total || 0,
        newAdoptedUsers: report.adoptions?.new || 0,
        totalContribution: report.contribution?.total || '0',
        newContribution: report.contribution?.growth || '0',
        totalDistributed: report.mining?.distributed || '0',
        dailyDistributed: report.mining?.distributed || '0',
        totalBurned: report.mining?.burned || '0',
        dailyBurned: report.mining?.burned || '0',
        openPrice: report.price?.open || '1',
        closePrice: report.price?.close || '1',
        highPrice: report.price?.high || '1',
        lowPrice: report.price?.low || '1',
        totalVolume: report.trading?.volume || '0',
        dailyVolume: report.trading?.volume || '0',
      })),
      total: result.total,
      page: result.pagination.page,
      pageSize: result.pagination.pageSize,
      totalPages: result.pagination.totalPages,
    };
  }
}
