import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Throttle } from '../../common/decorators/throttle.decorator';
import { PaginationQueryDto } from '../../common/dto/pagination-query.dto';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { ToggleFollowDto } from './dto/toggle-follow.dto';
import { FollowsService } from './follows.service';

@ApiTags('Follows')
@Controller('follows')
export class FollowsController {
  constructor(private readonly followsService: FollowsService) {}

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post('toggle')
  @Throttle(30, 60_000)
  async toggleFollow(@CurrentUser() user: JwtPayload, @Body() dto: ToggleFollowDto) {
    return this.followsService.toggleFollow(user.sub, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('followers/:userId')
  async followers(
    @CurrentUser() user: JwtPayload,
    @Param('userId') userId: string,
    @Query() query: PaginationQueryDto,
  ) {
    return this.followsService.getFollowers(userId, query, user.sub);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('following/:userId')
  async following(
    @CurrentUser() user: JwtPayload,
    @Param('userId') userId: string,
    @Query() query: PaginationQueryDto,
  ) {
    return this.followsService.getFollowing(userId, query, user.sub);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('status/:targetUserId')
  async status(@CurrentUser() user: JwtPayload, @Param('targetUserId') targetUserId: string) {
    return this.followsService.getFollowStatus(user.sub, targetUserId);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('suggestions')
  @Throttle(60, 60_000)
  async suggestions(@CurrentUser() user: JwtPayload, @Query() query: PaginationQueryDto) {
    return this.followsService.getSuggestions(user.sub, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('requests')
  async requests(@CurrentUser() user: JwtPayload, @Query() query: PaginationQueryDto) {
    return this.followsService.getPendingRequests(user.sub, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Patch('requests/:requestId/approve')
  async approveRequest(@CurrentUser() user: JwtPayload, @Param('requestId') requestId: string) {
    return this.followsService.approveRequest(user.sub, requestId);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Patch('requests/:requestId/reject')
  async rejectRequest(@CurrentUser() user: JwtPayload, @Param('requestId') requestId: string) {
    return this.followsService.rejectRequest(user.sub, requestId);
  }
}
