import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { SuperAdminPermissions } from '../../common/decorators/superadmin-permissions.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { SuperAdminPermissionsGuard } from '../../common/guards/superadmin-permissions.guard';
import { SuperAdminJwtAuthGuard } from '../../common/guards/super-admin-jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { AdminCommentQueryDto } from './dto/admin-comment-query.dto';
import { CommentQueryDto } from './dto/comment-query.dto';
import { CreateCommentDto } from './dto/create-comment.dto';
import { CreateCommentReplyDto } from './dto/create-comment-reply.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { CommentsService } from './comments.service';
import { SUPERADMIN_PERMISSIONS } from '../superadmin/superadmin-permissions';

@ApiTags('Comments')
@Controller('comments')
export class CommentsController {
  constructor(private readonly commentsService: CommentsService) {}

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post()
  async create(@CurrentUser() user: JwtPayload, @Body() dto: CreateCommentDto) {
    return this.commentsService.create(user.sub, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('post/:postId')
  async findByPost(
    @CurrentUser() user: JwtPayload,
    @Param('postId') postId: string,
    @Query() query: CommentQueryDto,
  ) {
    return this.commentsService.findByPost(user.sub, postId, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post(':commentId/replies')
  async createReply(
    @CurrentUser() user: JwtPayload,
    @Param('commentId') commentId: string,
    @Body() dto: CreateCommentReplyDto,
  ) {
    return this.commentsService.createReply(user.sub, commentId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get(':commentId/replies')
  async findReplies(
    @CurrentUser() user: JwtPayload,
    @Param('commentId') commentId: string,
    @Query() query: CommentQueryDto,
  ) {
    return this.commentsService.findReplies(user.sub, commentId, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Patch(':commentId/pin')
  async pin(@CurrentUser() user: JwtPayload, @Param('commentId') commentId: string) {
    return this.commentsService.pin(user.sub, commentId);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Patch(':commentId/unpin')
  async unpin(@CurrentUser() user: JwtPayload, @Param('commentId') commentId: string) {
    return this.commentsService.unpin(user.sub, commentId);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Patch(':commentId')
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('commentId') commentId: string,
    @Body() dto: UpdateCommentDto,
  ) {
    return this.commentsService.update(user.sub, commentId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  @Get('admin')
  async adminList(@Query() query: AdminCommentQueryDto) {
    return this.commentsService.findPlatformComments(query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Delete(':commentId')
  async remove(@CurrentUser() user: JwtPayload, @Param('commentId') commentId: string) {
    return this.commentsService.remove(user.sub, commentId);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  @Delete('admin/:commentId')
  async adminRemove(@CurrentUser() user: JwtPayload, @Param('commentId') commentId: string) {
    return this.commentsService.removeBySuperAdmin(user.email ?? user.sub, commentId);
  }
}
