import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
import { JwtAuthGuard } from "./jwt-auth.guard";
import { ItemsService } from "./items.service";

@Controller("items")
@UseGuards(JwtAuthGuard)
export class ItemsController {
  constructor(private readonly itemsService: ItemsService) {}

  @Get()
  findAll() {
    return this.itemsService.findAll();
  }

  @Get("search")
  searchItems(@Query("q") query: string) {
    return this.itemsService.searchItems(query || "");
  }

  @Get("by-code/:itemCode")
  findByCode(@Param("itemCode") itemCode: string) {
    return this.itemsService.findByCode(itemCode);
  }

  @Post()
  create(
    @Body()
    body: {
      itemCode: string;
      itemName: string;
      brand: string;
      barcode?: string;
      countryOfOrigin?: string;
      blend?: string;
      grainType?: string;
      varietyType?: string;
      processType?: string;
      unit?: string;
    },
  ) {
    return this.itemsService.create(body);
  }

  @Get(":id/batches")
  findBatches(@Param("id") id: string, @Query("warehouseId") warehouseId: string) {
    return this.itemsService.findBatches(id, warehouseId);
  }

  @Patch(":id")
  update(
    @Param("id") id: string,
    @Body()
    body: Partial<{
      itemCode: string;
      itemName: string;
      brand: string;
      barcode: string;
      countryOfOrigin: string;
      blend: string;
      grainType: string;
      varietyType: string;
      processType: string;
      unit: string;
      bagWeightKg: number;
    }>,
  ) {
    return this.itemsService.update(id, body);
  }
}
