import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from '@nestjs/common';
import { ClientsService } from './clients.service';
import { CreateClientDto } from './dto/create-client.dto';
import { UpdateClientDto } from './dto/update-client.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('clients')
@UseGuards(AuthGuard('jwt'))
export class ClientsController {
  constructor(private readonly clientsService: ClientsService) { }

  @Post()
  create(@Request() req, @Body() createClientDto: CreateClientDto) {
    if (!createClientDto.assignedTo) {
      createClientDto.assignedTo = req.user.id;
    }
    return this.clientsService.create(createClientDto);
  }

  @Get()
  findAll(@Request() req) {
    return this.clientsService.findAll(req.user);
  }

  @Get('test-500')
  throwError() {
    throw new Error('This is a test 500 error to verify the global exception filter');
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.clientsService.findOne(id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateClientDto: UpdateClientDto) {
    return this.clientsService.update(id, updateClientDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.clientsService.remove(id);
  }
}
