import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { DriverService } from './driver.service';
import { CreateDriverDto } from './dto/create-driver.dto';
import { UpdateDriverDto } from './dto/update-driver.dto';
import { AccountTypesGuard } from 'src/common/guards/accountTypes.guard';
import { AccountTypes } from 'src/common/decorators/accountType.decorator';
import { AccountType } from '@prisma/client';

@Controller('drivers')
@UseGuards(AccountTypesGuard)
@AccountTypes(AccountType.ADMIN, AccountType.PLANT)
export class DriverController {
  constructor(private readonly driverService: DriverService) { }

  @Post()
  create(@Body() createDriverDto: CreateDriverDto, @Req() req?: any,) {
    return this.driverService.create(createDriverDto, req.user);
  }

  @Get()
  findAll(
    @Query('page') page: string,
    @Query('limit') limit: string,
    @Query('q') query?: string,
    @Query('warehouseId') warehouseId?: string,
    @Req() req?: any,
  ) {
    const pageNumber = parseInt(page ?? '1');
    const perPage = parseInt(limit ?? '10');
    return this.driverService.findAll(pageNumber, perPage, query, warehouseId, req.user);
  }

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

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateDriverDto: UpdateDriverDto, @Req() req?: any) {
    return this.driverService.update(id, updateDriverDto, req.user);
  }

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