import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  Param,
  Patch,
  Post,
  Query,
  Req,
  UseGuards,
} from "@nestjs/common";
import type { Request } from "express";
import { ProjectsService } from "../services/projects.service.js";
import { JwtAuthGuard } from "../auth/jwt-auth.guard.js";
import {
  CreateProjectDto,
  ProjectDto,
  QueryProjectDto,
  UpdateProjectDto,
} from "@sigma/common";
import type { ProjectDocument } from "../schemas/project.schema.js";

@Controller("projects")
export class ProjectsController {
  constructor(private readonly projectsService: ProjectsService) {}

  @UseGuards(JwtAuthGuard)
  @Post()
  create(
    @Req() request: Request,
    @Body() createProjectDto: CreateProjectDto
  ): Promise<ProjectDocument> {
    return this.projectsService.create(request.user.userId, createProjectDto);
  }

  @UseGuards(JwtAuthGuard)
  @Get()
  findAll(
    @Req() request: Request,
    @Query() query: QueryProjectDto
  ): Promise<ProjectDocument[]> {
    return this.projectsService.findAll(request.user.userId, query);
  }

  @UseGuards(JwtAuthGuard)
  @Get(":id")
  findOne(
    @Req() request: Request,
    @Param("id") id: string
  ): Promise<ProjectDto | null> {
    return this.projectsService.findOne(request.user.userId, id);
  }

  @UseGuards(JwtAuthGuard)
  @Patch(":id")
  update(
    @Req() request: Request,
    @Param("id") id: string,
    @Body() updateProjectDto: UpdateProjectDto
  ): Promise<ProjectDocument | null> {
    return this.projectsService.update(
      request.user.userId,
      id,
      updateProjectDto
    );
  }

  @UseGuards(JwtAuthGuard)
  @Delete(":id")
  @HttpCode(204)
  remove(
    @Req() request: Request,
    @Param("id") id: string
  ): Promise<ProjectDocument | null> {
    return this.projectsService.remove(request.user.userId, id);
  }
}
