import { Controller, Get, Post, Put, Delete, 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('community')
export class CommunityController {
  constructor(private communityService: CommunityService) {}

  @Get('posts')
  async findPosts(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('tag') tag?: string,
    @Query('circleId') circleId?: string,
    @Query('userId') userId?: string,
  ) {
    return this.communityService.findPosts({
      page: page ? parseInt(page) : undefined,
      pageSize: pageSize ? parseInt(pageSize) : undefined,
      tag,
      circleId: circleId ? parseInt(circleId) : undefined,
      userId: userId ? parseInt(userId) : undefined,
    });
  }

  @Get('posts/:id')
  async findPostById(@Param('id') id: string) {
    return this.communityService.findPostById(parseInt(id));
  }

  @Post('posts')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async createPost(@Req() req: any, @Body() body: { title: string; content: string; tags?: string; circleId?: number }) {
    return this.communityService.createPost(req.user.userId, body);
  }

  @Put('posts/:id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async updatePost(@Req() req: any, @Param('id') id: string, @Body() body: { title?: string; content?: string; tags?: string }) {
    return this.communityService.updatePost(req.user.userId, parseInt(id), body);
  }

  @Delete('posts/:id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async deletePost(@Req() req: any, @Param('id') id: string) {
    return this.communityService.deletePost(req.user.userId, parseInt(id));
  }

  @Post('posts/:id/comments')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async addComment(
    @Req() req: any,
    @Param('id') id: string,
    @Body('content') content: string,
    @Body('parentId') parentId?: number,
  ) {
    return this.communityService.addComment(req.user.userId, parseInt(id), content, parentId);
  }

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

  @Get('posts/:id/like')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async checkLike(@Req() req: any, @Param('id') id: string) {
    return this.communityService.checkLike(req.user.userId, parseInt(id));
  }

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

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

  @Delete('users/:id/follow')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async unfollowUser(@Req() req: any, @Param('id') id: string) {
    return this.communityService.unfollowUser(req.user.userId, parseInt(id));
  }

  @Get('users/:id/follow')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async checkFollow(@Req() req: any, @Param('id') id: string) {
    return this.communityService.checkFollow(req.user.userId, parseInt(id));
  }

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

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

  @Get('users/:id/profile')
  async getUserProfile(@Param('id') id: string) {
    return this.communityService.getUserProfile(parseInt(id));
  }
}
