import { Body, Controller, ParseArrayPipe, Post, UseGuards } from "@nestjs/common";
import type { AuthUser } from "@mosaic/shared";
import { CurrentUser } from "../auth/decorators/current-user.decorator";
import { AdminGuard } from "../auth/guards/admin.guard";
import { AuthGuard } from "../auth/guards/auth.guard";
import { Workspace } from "../common/decorators";
import { WorkspaceGuard } from "../common/guards";
import { ImportProjectDto, type ImportResponseDto, ImportTaskDto } from "./dto";
import { ImportService } from "./import.service";

@Controller("import")
@UseGuards(AuthGuard, WorkspaceGuard, AdminGuard)
export class ImportController {
  constructor(private readonly importService: ImportService) {}

  @Post("tasks")
  async importTasks(
    @Body(new ParseArrayPipe({ items: ImportTaskDto })) taskPayload: ImportTaskDto[],
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthUser
  ): Promise<ImportResponseDto> {
    return this.importService.importTasks(workspaceId, user.id, taskPayload);
  }

  @Post("projects")
  async importProjects(
    @Body(new ParseArrayPipe({ items: ImportProjectDto })) projectPayload: ImportProjectDto[],
    @Workspace() workspaceId: string,
    @CurrentUser() user: AuthUser
  ): Promise<ImportResponseDto> {
    return this.importService.importProjects(workspaceId, user.id, projectPayload);
  }
}
