import { Controller, Get, Query, UseGuards, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { SellerJwtAuthGuard } from '@/common/guards/seller-jwt-auth.guard';
import { CurrentSeller } from '@/common/decorators/current-seller.decorator';
import { MerchantStatisticsService } from './statistics.service';
import { MerchantProfileService } from '../profile/profile.service';

@ApiTags('商家统计')
@ApiBearerAuth()
@UseGuards(SellerJwtAuthGuard)
@Controller('merchant/statistics')
export class MerchantStatisticsController {
  constructor(
    private readonly statisticsService: MerchantStatisticsService,
    private readonly profileService: MerchantProfileService,
  ) {}

  @Get('overview')
  @ApiOperation({ summary: '获取数据概览' })
  async getOverview(@CurrentSeller('sub') sellerId: number) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.statisticsService.getOverview(merchant.id);
  }

  @Get('income-trend')
  @ApiOperation({ summary: '获取收入趋势' })
  async getIncomeTrend(
    @CurrentSeller('sub') sellerId: number,
    @Query('type') type: 'day' | 'week' | 'month' = 'day',
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.statisticsService.getIncomeTrend(merchant.id, type);
  }
}
