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

@Controller('dms')
@UseGuards(JwtAuthGuard)
export class DmsController {
  constructor(private readonly dmsService: DmsService) {}

  @Post()
  async createOrGetDm(
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: { user_id: string },
  ) {
    return this.dmsService.createOrGetDm(userId, dto.user_id, wsId);
  }

  @Get()
  async listDms(
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
  ) {
    return this.dmsService.listDms(userId, wsId);
  }

  @Get(':id/messages')
  async getMessages(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
    @Query() query: any,
  ) {
    return this.dmsService.getDmMessages(id, userId, query);
  }

  @Post('group')
  async createGroupDm(
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: { user_ids: string[]; name?: string },
  ) {
    return this.dmsService.createGroupDm(userId, dto.user_ids, wsId, dto.name);
  }

  @Post(':id/messages')
  async sendMessage(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: { text: string },
  ) {
    return this.dmsService.sendDmMessage(id, userId, wsId, dto.text);
  }
}