import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Patch,
  Post,
  Query,
  Request,
  UseGuards,
} from '@nestjs/common';
import { Types } from 'mongoose';
import { ProjectService } from './project.service';
import { CreateProjectDto } from './dto/create-project.dto';
import { UpdateProjectDto } from './dto/update-project.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller({
  path: 'project',
  version: '1',
})
export class ProjectController {
  constructor(private readonly projectService: ProjectService) {}

  @UseGuards(JwtAuthGuard)
  @Post()
  create(@Request() req, @Body() createProjectDto: CreateProjectDto) {
    return this.projectService.create(createProjectDto, req.user);
  }

  @Post('without-auth')
  createWithoutAuth(@Body() createProjectDto: CreateProjectDto) {
    return this.projectService.createWithoutAuth(createProjectDto);
  }

  @UseGuards(JwtAuthGuard)
  @Get('allProjects')
  findAll(@Request() req) {
    return this.projectService.findAll(req.user);
  }

  @UseGuards(JwtAuthGuard)
  @Get()
  findOne(@Query('projectID') id: string) {
    if (!Types.ObjectId.isValid(id)) {
      throw new BadRequestException('projectID is not a valid MongoId');
    }

    return this.projectService.findOne(id);
  }

  @UseGuards(JwtAuthGuard)
  @Patch()
  update(
    @Query('projectID') id: string,
    @Body() updateProjectDto: UpdateProjectDto,
  ) {
    return this.projectService.update(id, updateProjectDto);
  }

  @UseGuards(JwtAuthGuard)
  @Delete()
  remove(@Query('projectID') id: string) {
    if (!Types.ObjectId.isValid(id)) {
      throw new BadRequestException('projectID is not a valid MongoId');
    }

    return this.projectService.remove(id);
  }

  @UseGuards(JwtAuthGuard)
  @Post('remove')
  removeMany(@Body('ids') ids: string[]) {
    return this.projectService.removeMany(ids);
  }
}
