import {
  Controller,
  Get,
  Post,
  Delete,
  Put,
  Body,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { WatchlistService } from './watchlist.service';

@ApiTags('自选股')
@Controller('watchlist')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class WatchlistController {
  constructor(private readonly watchlistService: WatchlistService) {}

  @Get()
  @ApiOperation({ summary: '获取自选股列表' })
  async getWatchlist(@CurrentUser('userId') userId: number) {
    return this.watchlistService.getWatchlist(userId);
  }

  @Post()
  @ApiOperation({ summary: '添加自选股' })
  async addToWatchlist(
    @CurrentUser('userId') userId: number,
    @Body() dto: { symbol: string; alertPrice?: number },
  ) {
    return this.watchlistService.addToWatchlist(
      userId,
      dto.symbol,
      dto.alertPrice,
    );
  }

  @Delete(':symbol')
  @ApiOperation({ summary: '删除自选股' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async removeFromWatchlist(
    @CurrentUser('userId') userId: number,
    @Param('symbol') symbol: string,
  ) {
    await this.watchlistService.removeFromWatchlist(userId, symbol);
    return { message: '删除成功' };
  }

  @Put(':symbol/alert')
  @ApiOperation({ summary: '更新价格预警' })
  @ApiParam({ name: 'symbol', description: '品种代码' })
  async updateAlertPrice(
    @CurrentUser('userId') userId: number,
    @Param('symbol') symbol: string,
    @Body() dto: { alertPrice: number },
  ) {
    return this.watchlistService.updateAlertPrice(
      userId,
      symbol,
      dto.alertPrice,
    );
  }
}
