import { Body, Controller, Delete, Get, Param, Post, UseGuards } from "@nestjs/common";
import { AuthGuard } from "../auth/guards/auth.guard";
import { PermissionGuard, WorkspaceGuard } from "../common/guards";
import { Permission, RequirePermission, Workspace } from "../common/decorators";
import { CreateTeamDto } from "./dto/create-team.dto";
import { ManageTeamMemberDto } from "./dto/manage-team-member.dto";
import { TeamsService } from "./teams.service";

@Controller("workspaces/:workspaceId/teams")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class TeamsController {
  constructor(private readonly teamsService: TeamsService) {}

  @Post()
  @RequirePermission(Permission.WORKSPACE_ADMIN)
  async create(@Body() createTeamDto: CreateTeamDto, @Workspace() workspaceId: string) {
    return this.teamsService.create(workspaceId, createTeamDto);
  }

  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findAll(@Workspace() workspaceId: string) {
    return this.teamsService.findAll(workspaceId);
  }

  @Post(":teamId/members")
  @RequirePermission(Permission.WORKSPACE_ADMIN)
  async addMember(
    @Param("teamId") teamId: string,
    @Body() dto: ManageTeamMemberDto,
    @Workspace() workspaceId: string
  ) {
    return this.teamsService.addMember(workspaceId, teamId, dto);
  }

  @Delete(":teamId/members/:userId")
  @RequirePermission(Permission.WORKSPACE_ADMIN)
  async removeMember(
    @Param("teamId") teamId: string,
    @Param("userId") userId: string,
    @Workspace() workspaceId: string
  ) {
    return this.teamsService.removeMember(workspaceId, teamId, userId);
  }

  @Delete(":teamId")
  @RequirePermission(Permission.WORKSPACE_ADMIN)
  async remove(@Param("teamId") teamId: string, @Workspace() workspaceId: string) {
    return this.teamsService.remove(workspaceId, teamId);
  }
}
