import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { CreateInventoryDto } from './dto/create-inventory.dto';
import { UpdateInventoryDto } from './dto/update-inventory.dto';
import { InventoryService } from './inventory.service';
import { ConsumeInventoryDto } from './dto/consume-inventory.dto';
import { CurrentUser } from '../auth/decorators/current-user.decorator';

@Controller('inventory')
export class InventoryController {
  constructor(private readonly inventoryService: InventoryService) {}
 
 @Post(':id/consume')
 consume(
   @CurrentUser() user: { userId: number },
   @Param('id', ParseIntPipe) id: number,
   @Body() body: ConsumeInventoryDto,
  ) {
  return this.inventoryService.consume(id, user.userId, body);
  }
  
  @Get(':id/consumption-history')
findConsumptionHistory(
  @CurrentUser() user: { userId: number },
  @Param('id', ParseIntPipe) id: number,
) {
  return this.inventoryService.findConsumptionHistory(id, user.userId);
  } 
 
  @Get()
  findAll(
    @CurrentUser() user: { userId: number },
    @Query('location') location?: string,
    @Query('sort') sort?: string,
  ) {
    return this.inventoryService.findAll(user.userId, { location, sort });
  }

  @Get('expiring')
  findExpiring(@CurrentUser() user: { userId: number }) {
    return this.inventoryService.findExpiring(user.userId);
  }

  @Post()
  create(
    @CurrentUser() user: { userId: number },
    @Body() body: CreateInventoryDto,
  ) {
    return this.inventoryService.create(user.userId, body);
  }

  @Patch(':id')
  update(
    @CurrentUser() user: { userId: number },
    @Param('id', ParseIntPipe) id: number,
    @Body() body: UpdateInventoryDto,
  ) {
    return this.inventoryService.update(id, user.userId, body);
  }

  @Delete(':id')
  remove(
    @CurrentUser() user: { userId: number },
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.inventoryService.remove(id, user.userId);
  }
}