import { Controller, Get, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
import { IndicatorsService } from './indicators.service';
import { GetIndicatorsDto } from './dto/indicator.dto';

@ApiTags('技术指标')
@Controller('indicators')
export class IndicatorsController {
  constructor(private readonly indicatorsService: IndicatorsService) {}

  @Get(':symbol')
  @ApiOperation({ summary: '获取技术指标' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getIndicators(
    @Param('symbol') symbol: string,
    @Query() dto: GetIndicatorsDto,
  ) {
    const results = await this.indicatorsService.calculateAllIndicators(
      symbol,
      dto.period,
    );

    // 如果指定了特定指标，只返回这些指标
    if (dto.indicators && dto.indicators.length > 0) {
      const filtered: any = {};
      dto.indicators.forEach((ind) => {
        if (results[ind]) {
          filtered[ind] = results[ind];
        }
      });
      return filtered;
    }

    return results;
  }

  @Get(':symbol/macd')
  @ApiOperation({ summary: '获取MACD指标' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async getMACD(
    @Param('symbol') symbol: string,
    @Query('period') period: string = '1d',
  ) {
    const klineData = await this.indicatorsService['marketService'].getKlineData(
      symbol,
      period,
      200,
    );
    return this.indicatorsService.calculateMACD(klineData);
  }

  @Get(':symbol/rsi')
  @ApiOperation({ summary: '获取RSI指标' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async getRSI(
    @Param('symbol') symbol: string,
    @Query('period') period: string = '1d',
    @Query('rsiPeriod') rsiPeriod: number = 14,
  ) {
    const klineData = await this.indicatorsService['marketService'].getKlineData(
      symbol,
      period,
      200,
    );
    return this.indicatorsService.calculateRSI(klineData, rsiPeriod);
  }

  @Get(':symbol/bollinger')
  @ApiOperation({ summary: '获取布林带指标' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async getBollinger(
    @Param('symbol') symbol: string,
    @Query('period') period: string = '1d',
  ) {
    const klineData = await this.indicatorsService['marketService'].getKlineData(
      symbol,
      period,
      200,
    );
    return this.indicatorsService.calculateBollinger(klineData);
  }

  @Get(':symbol/kdj')
  @ApiOperation({ summary: '获取KDJ指标' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async getKDJ(
    @Param('symbol') symbol: string,
    @Query('period') period: string = '1d',
  ) {
    const klineData = await this.indicatorsService['marketService'].getKlineData(
      symbol,
      period,
      200,
    );
    return this.indicatorsService.calculateKDJ(klineData);
  }
}
