import {
  Controller,
  Get,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { ReportService } from '@/modules/shared/finance/report.service';
import { JwtAuthGuard, RolesGuard } from '@/common';
import { Roles } from '@/common/decorators/roles.decorator';
import {
  QueryTrendDto,
  QueryDailyReportDto,
  QueryWeeklyReportDto,
  QueryMonthlyReportDto,
} from '@/modules/shared/finance/dto/report.dto';

@ApiTags('财务报表(管理员)')
@Controller('admin/finance/reports')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@ApiBearerAuth()
export class ReportAdminController {
  constructor(private readonly reportService: ReportService) {}

  @Get('overview')
  @ApiOperation({ summary: '财务总览' })
  async getOverview() {
    return this.reportService.getOverview();
  }

  @Get('trend')
  @ApiOperation({ summary: '财务趋势' })
  async getTrend(@Query() dto: QueryTrendDto) {
    const startDate = dto.startDate || new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
    const endDate = dto.endDate || new Date().toISOString().split('T')[0];
    return this.reportService.getTrend({ startDate, endDate });
  }

  @Get('daily')
  @ApiOperation({ summary: '日报表' })
  async getDailyReport(@Query() dto: QueryDailyReportDto) {
    const date = dto.date || new Date().toISOString().split('T')[0];
    return this.reportService.getDailyReport(date);
  }

  @Get('weekly')
  @ApiOperation({ summary: '周报表' })
  async getWeeklyReport(@Query() dto: QueryWeeklyReportDto) {
    const endDate = dto.endDate || new Date().toISOString().split('T')[0];
    const startDate = dto.startDate || new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
    return this.reportService.getWeeklyReport({ startDate, endDate });
  }

  @Get('monthly')
  @ApiOperation({ summary: '月报表' })
  async getMonthlyReport(@Query() dto: QueryMonthlyReportDto) {
    return this.reportService.getMonthlyReport({
      year: dto.year,
      month: dto.month,
    });
  }

  @Get('export')
  @ApiOperation({ summary: '导出报表' })
  async exportReport(@Query() dto: QueryWeeklyReportDto) {
    // TODO: 实现导出功能
    return { message: '导出功能开发中' };
  }
}
