import { Controller, Get, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import { MarketService } from './market.service';
import { GetProductsDto, GetKlineDto } from './dto/market.dto';

@ApiTags('行情')
@Controller('market')
export class MarketController {
  constructor(private readonly marketService: MarketService) {}

  @Get('products')
  @ApiOperation({ summary: '获取品种列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getProducts(@Query() dto: GetProductsDto) {
    return this.marketService.getProducts(dto.category, dto.page, dto.size);
  }

  @Get('products/:symbol')
  @ApiOperation({ summary: '获取品种详情' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '品种不存在' })
  async getProduct(@Param('symbol') symbol: string) {
    return this.marketService.getProduct(symbol);
  }

  @Get('products/:symbol/kline')
  @ApiOperation({ summary: '获取K线数据' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getKline(
    @Param('symbol') symbol: string,
    @Query() dto: GetKlineDto,
  ) {
    return this.marketService.getKlineData(
      symbol,
      dto.period,
      dto.count,
      dto.startTime ? new Date(dto.startTime) : undefined,
      dto.endTime ? new Date(dto.endTime) : undefined,
    );
  }

  @Get('products/:symbol/tick')
  @ApiOperation({ summary: '获取实时行情' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getTick(@Param('symbol') symbol: string) {
    return this.marketService.getTickData(symbol);
  }

  @Get('overview')
  @ApiOperation({ summary: '获取市场概览' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getOverview() {
    return this.marketService.getMarketOverview();
  }
}
