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

@Controller('user-groups')
@UseGuards(JwtAuthGuard)
export class UserGroupsController {
  constructor(private readonly userGroupsService: UserGroupsService) {}

  @Get()
  async list(@Workspace('id') wsId: string) {
    return this.userGroupsService.list(wsId);
  }

  @Post()
  async create(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { name: string; handle: string; description?: string },
  ) {
    return this.userGroupsService.create(wsId, userId, dto);
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Workspace('id') wsId: string,
    @Body() dto: { name?: string; handle?: string; description?: string },
  ) {
    return this.userGroupsService.update(id, wsId, dto);
  }

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

  @Post(':id/members')
  async addMember(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { user_id: string },
  ) {
    return this.userGroupsService.addMember(id, dto.user_id, userId);
  }

  @Delete(':id/members/:userId')
  async removeMember(
    @Param('id') id: string,
    @Param('userId') memberUserId: string,
  ) {
    return this.userGroupsService.removeMember(id, memberUserId);
  }
}