import { Controller, Get, Query, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { GetPriceQuery } from '../../application/queries/get-price.query';

@ApiTags('Price')
@Controller('price')
export class PriceController {
  constructor(private readonly getPriceQuery: GetPriceQuery) {}

  @Get('current')
  @ApiOperation({ summary: '获取当前价格' })
  @ApiResponse({ status: 200, description: '当前价格' })
  async getCurrentPrice() {
    const price = await this.getPriceQuery.getCurrentPrice();
    if (!price) {
      throw new NotFoundException('Price not available');
    }
    return price;
  }

  @Get('history')
  @ApiOperation({ summary: '获取价格历史' })
  @ApiQuery({ name: 'startTime', required: true, description: '开始时间 (ISO 8601)' })
  @ApiQuery({ name: 'endTime', required: true, description: '结束时间 (ISO 8601)' })
  @ApiQuery({ name: 'interval', required: false, enum: ['minute', 'hour', 'day'], description: '时间间隔' })
  @ApiResponse({ status: 200, description: '价格历史' })
  async getPriceHistory(
    @Query('startTime') startTime: string,
    @Query('endTime') endTime: string,
    @Query('interval') interval?: 'minute' | 'hour' | 'day',
  ) {
    const history = await this.getPriceQuery.getPriceHistory(
      new Date(startTime),
      new Date(endTime),
      interval ?? 'hour',
    );
    return { data: history };
  }

  @Get('kline')
  @ApiOperation({ summary: '获取K线数据' })
  @ApiQuery({ name: 'startTime', required: true, description: '开始时间 (ISO 8601)' })
  @ApiQuery({ name: 'endTime', required: true, description: '结束时间 (ISO 8601)' })
  @ApiQuery({ name: 'interval', required: false, enum: ['minute', 'hour', 'day'], description: '时间间隔' })
  @ApiResponse({ status: 200, description: 'K线数据' })
  async getKLineData(
    @Query('startTime') startTime: string,
    @Query('endTime') endTime: string,
    @Query('interval') interval?: 'minute' | 'hour' | 'day',
  ) {
    const kLine = await this.getPriceQuery.getKLineData(
      new Date(startTime),
      new Date(endTime),
      interval ?? 'hour',
    );
    return { data: kLine };
  }
}
