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

@ApiTags('圈子')
@Controller('circles')
export class CirclesController {
  constructor(private communityService: CommunityService) {}

  @Get()
  async findCircles(@Query('category') category?: string) {
    return this.communityService.findCircles(category);
  }

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

  @Get(':id/posts')
  async findCirclePosts(
    @Param('id') id: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    return this.communityService.findCirclePosts(+id, {
      page: page ? parseInt(page) : undefined,
      pageSize: pageSize ? parseInt(pageSize) : undefined,
    });
  }

  @Post()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async createCircle(@Req() req: any, @Body() body: { name: string; description?: string; tags?: string }) {
    return this.communityService.createCircle(req.user.userId, body);
  }

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

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

  @Get(':id/membership')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async checkMembership(@Req() req: any, @Param('id') id: string) {
    return this.communityService.checkCircleMembership(req.user.userId, +id);
  }
}
