import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req } from '@nestjs/common';
import { InventoryService } from './inventory.service';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
// import { Roles } from '../../common/decorators/roles.decorator';
import { PaginationDto } from '../../common/dto/pagination.dto';

@ApiTags('products')
@Controller('products')
export class UsersController {
  constructor(private readonly inventoryService: InventoryService) {}

  @Get('/store')
  // @Roles('admin')
  @ApiOperation({ summary: 'Get all products with pagination and filters' })
  @ApiResponse({ status: 200, description: 'Return paginated products.' })
  async findAll(@Query() paginationDto: PaginationDto) {
    const result = await this.inventoryService.findAll(paginationDto,true);
    return { 
      message: 'products fetched successfully', 
      data: result.data,
      meta: result.meta
    };
  }

  @Get('/inventory')
  // @Roles('admin')
  @ApiOperation({ summary: 'Get all products with pagination and filters' })
  @ApiResponse({ status: 200, description: 'Return paginated products.' })
  async findAllByUserId(@Req() req: Request, @Query() paginationDto: PaginationDto) {
    // console.log(req['user'].id)
    // const id = 1
    const id = Number(req['user'].id);
    const result = await this.inventoryService.findAllByUserId(id,paginationDto);
    return { 
      message: 'products fetched successfully', 
      data: result.data,
      meta: result.meta
    };
  }

  @Get('/id/:id')
  // @Roles('admin')
  @ApiOperation({ summary: 'Get a product by ID' })
  @ApiResponse({ status: 200, description: 'Return the product.' })
  @ApiResponse({ status: 404, description: 'product not found.' })
  async findOne(@Param('id') id: string) {
    const data = await this.inventoryService.findOne(id);
    return { message: 'product fetched successfully', data };
  }

  @Post()
  // @Roles('admin')
  @ApiOperation({ summary: 'Create a new product' })
  @ApiResponse({ status: 201, description: 'Product created successfully.' })
  async create(
    @Body() createUserDto: CreateProductDto,
    @Query('roleId') roleId?: string,
  ) {
    const data = await this.inventoryService.create(createUserDto)
    return { message: 'User created successfully', data };
  }

  @Patch(':id')
  // @Roles('admin')
  @ApiOperation({ summary: 'Update a product' })
  @ApiResponse({ status: 200, description: 'Product updated successfully.' })
  @ApiResponse({ status: 404, description: 'Product not found.' })
  async update(@Param('id') id: string, @Body() UpdateProductDto: UpdateProductDto) {
    const data = await this.inventoryService.update(id, UpdateProductDto);
    return { message: 'User updated successfully', data };
  }

  // @Delete(':id')
  // @Roles('admin')
  // @ApiOperation({ summary: 'Delete a user' })
  // @ApiResponse({ status: 200, description: 'User deleted successfully.' })
  // @ApiResponse({ status: 404, description: 'User not found.' })
  // async remove(@Param('id') id: string) {
  //   return await this.inventoryService.remove(id);
  // }
}
