import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Post,
  Query,
  Put,
} from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";

import { FreightAdmin } from "../../common/booking-guards";
import { BackofficeService } from "./backoffice.service";
import { CreateOrganizationUserDto } from "./dto/create-organization-user.dto";
import { UpdateEmployeeUserRolesDto } from "./dto/update-employee-user-roles.dto";

@ApiTags("backoffice")
@Controller("backoffice")
@FreightAdmin()
export class BackofficeController {
  constructor(private readonly backofficeService: BackofficeService) {}

  @Post("organizations/:orgId/users")
  @ApiOperation({ summary: "Create an organization user without assigning positions" })
  createOrganizationUser(
    @Param("orgId", ParseUUIDPipe) organizationId: string,
    @Body() dto: CreateOrganizationUserDto,
  ) {
    return this.backofficeService.createOrganizationUser(organizationId, dto);
  }

  @Get("organizations/:orgId/employees")
  @ApiOperation({ summary: "Get deduplicated organization employees for backoffice" })
  getOrganizationEmployees(
    @Param("orgId", ParseUUIDPipe) organizationId: string,
    @Query("skip") skip?: string,
    @Query("take") take?: string,
  ) {
    return this.backofficeService.getOrganizationEmployees(organizationId, {
      skip,
      take,
    });
  }

  @Get("organizations/:orgId/employee-users/:userId/roles")
  @ApiOperation({ summary: "Get org-scoped roles assigned to an employee user" })
  getEmployeeUserRoles(
    @Param("orgId", ParseUUIDPipe) organizationId: string,
    @Param("userId", ParseUUIDPipe) userId: string,
  ) {
    return this.backofficeService.getEmployeeUserRoles(organizationId, userId);
  }

  @Put("organizations/:orgId/employee-users/:userId/roles")
  @ApiOperation({ summary: "Replace org-scoped roles assigned to an employee user" })
  replaceEmployeeUserRoles(
    @Param("orgId", ParseUUIDPipe) organizationId: string,
    @Param("userId", ParseUUIDPipe) userId: string,
    @Body() dto: UpdateEmployeeUserRolesDto,
  ) {
    return this.backofficeService.replaceEmployeeUserRoles(
      organizationId,
      userId,
      dto.roleIds,
    );
  }
}
