import { Controller, Get, Post, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { PromptsService } from './prompts.service';

@ApiTags('提示词')
@Controller('prompts')
export class PromptsController {
  constructor(private promptsService: PromptsService) {}

  @Get()
  async findAll(@Query() query: { page?: number; pageSize?: number; categoryId?: number; search?: string }) {
    return this.promptsService.findAll(query);
  }

  @Get(':id')
  async findById(@Param('id') id: string) {
    return this.promptsService.findById(+id);
  }

  @Post()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async create(@Req() req: any, @Body() body: { title: string; content: string; description?: string; categoryId?: number; tags?: string; model?: string }) {
    return this.promptsService.create({ ...body, authorId: req.user.userId });
  }

  @Post(':id/favorite')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async toggleFavorite(@Req() req: any, @Param('id') id: string) {
    return this.promptsService.toggleFavorite(req.user.userId, +id);
  }

  @Get('favorites')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async findFavorites(@Req() req: any, @Query('page') page?: string, @Query('pageSize') pageSize?: string) {
    return this.promptsService.findFavorites(req.user.userId, {
      page: page ? parseInt(page) : undefined,
      pageSize: pageSize ? parseInt(pageSize) : undefined,
    });
  }
}
