import { Controller, Get, Post, Patch, Delete, Body, Param, UseGuards } from "@nestjs/common";
import { LayoutsService } from "./layouts.service";
import { CreateLayoutDto, UpdateLayoutDto } from "./dto";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";
import { CurrentUser } from "../auth/decorators/current-user.decorator";
import type { AuthenticatedUser } from "../common/types/user.types";

@Controller("layouts")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class LayoutsController {
  constructor(private readonly layoutsService: LayoutsService) {}

  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findAll(@Workspace() workspaceId: string, @CurrentUser() user: AuthenticatedUser) {
    return this.layoutsService.findAll(workspaceId, user.id);
  }

  @Get("default")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findDefault(@Workspace() workspaceId: string, @CurrentUser() user: AuthenticatedUser) {
    return this.layoutsService.findDefault(workspaceId, user.id);
  }

  @Get(":id")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findOne(
    @Param("id") id: string,
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthenticatedUser
  ) {
    return this.layoutsService.findOne(id, workspaceId, user.id);
  }

  @Post()
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async create(
    @Body() createLayoutDto: CreateLayoutDto,
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthenticatedUser
  ) {
    return this.layoutsService.create(workspaceId, user.id, createLayoutDto);
  }

  @Patch(":id")
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async update(
    @Param("id") id: string,
    @Body() updateLayoutDto: UpdateLayoutDto,
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthenticatedUser
  ) {
    return this.layoutsService.update(id, workspaceId, user.id, updateLayoutDto);
  }

  @Delete(":id")
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async remove(
    @Param("id") id: string,
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthenticatedUser
  ) {
    return this.layoutsService.remove(id, workspaceId, user.id);
  }
}
