import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { LearningActivityService } from './learning-activity.service';
import { GrowthService } from './growth.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import type { UserPayload } from '../../common/types';

@ApiTags('learning-activity')
@Controller('activity')
export class LearningActivityController {
  constructor(
    private readonly activityService: LearningActivityService,
    private readonly growth: GrowthService,
  ) {}

  @Get('heatmap')
  @ApiOperation({ summary: '获取学习热力图数据' })
  async getHeatmap(
    @CurrentUser() user: UserPayload,
    @Query('days') days?: string,
  ) {
    const d = Math.min(Number(days ?? 365) || 365, 365);
    return this.activityService.getHeatmap(String(user?.id || 'anonymous'), d);
  }

  @Get('summary')
  @ApiOperation({ summary: '获取学习统计概览' })
  async getSummary(@CurrentUser() user: UserPayload) {
    return this.activityService.getSummary(String(user?.id || 'anonymous'));
  }

  @Get('trend')
  @ApiOperation({ summary: '获取 AI 学习趋势分析' })
  async getTrend(@CurrentUser() user: UserPayload, @Query('days') days?: string) {
    const periodDays = parseInt(days || '7', 10);
    return this.activityService.getTrend(String(user?.id || 'anonymous'), Math.min(Math.max(periodDays, 7), 30));
  }

  @Get('streak')
  @ApiOperation({ summary: '获取连续学习天数' })
  async getStreak(@CurrentUser() user: UserPayload) {
    return this.growth.getStreak(String(user?.id || 'anonymous'));
  }

  @Get('recommendations')
  @ApiOperation({ summary: '获取下一步学习推荐' })
  async getRecommendations(@CurrentUser() user: UserPayload) {
    return this.growth.getRecommendations(String(user?.id || 'anonymous'));
  }
}
