import { Body, Controller, Post, Patch, Delete, Get, Param, UseGuards } from '@nestjs/common';
import { MessagesService } from './messages.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('messages')
@UseGuards(JwtAuthGuard)
export class MessagesController {
  constructor(private readonly messagesService: MessagesService) {}

  @Post(':channelId')
  async send(@Param('channelId') channelId: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string, @Body() dto: any) {
    return this.messagesService.send(channelId, userId, wsId, dto);
  }

  @Patch(':id')
  async edit(@Param('id') id: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string, @Body() dto: any) {
    return this.messagesService.edit(id, userId, wsId, dto);
  }

  @Delete(':id')
  async delete(@Param('id') id: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string) {
    return this.messagesService.delete(id, userId, wsId);
  }

  @Get(':id/thread')
  async thread(@Param('id') id: string, @Workspace('id') wsId: string) {
    return this.messagesService.getThread(id, wsId);
  }

  @Post(':id/reactions')
  async addReaction(@Param('id') id: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string, @Body() dto: any) {
    return this.messagesService.addReaction(id, userId, dto.emoji, wsId);
  }

  @Delete(':id/reactions/:emoji')
  async removeReaction(@Param('id') id: string, @Param('emoji') emoji: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string) {
    return this.messagesService.removeReaction(id, userId, decodeURIComponent(emoji), wsId);
  }

  @Post(':id/save')
  async save(@Param('id') id: string, @CurrentUser('userId') userId: string) {
    return this.messagesService.save(id, userId);
  }

  @Delete(':id/save')
  async unsave(@Param('id') id: string, @CurrentUser('userId') userId: string) {
    return this.messagesService.unsave(id, userId);
  }
}