import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, Query, DefaultValuePipe, ParseArrayPipe } from '@nestjs/common';
import { ProductsService } from './products.service';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { ApiCreatedResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { ProductEntity } from './entities/product.entity';

@Controller('products')
@ApiTags('products')
export class ProductsController {
	constructor(private readonly productsService: ProductsService) {}

	@Post()
	@ApiCreatedResponse({ type: ProductEntity })
	create(@Body() createProductDto: CreateProductDto) {
		return this.productsService.create(createProductDto);
	}

	@Get()
	@ApiOkResponse({ type: ProductEntity, isArray: true })
	findAll(@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number, @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number, @Query('sortBy') sortBy: string, @Query('sortOrder') sortOrder: string, @Query('search') search: string) {
		return this.productsService.findAll((page - 1) * limit, limit, sortBy, sortOrder, search);
	}

	@Get('/simple')
	@ApiOkResponse({ type: ProductEntity, isArray: true })
	findAllSimple(@Query() params: any) {
        return this.productsService.findAllSimple(params);
	}

	@Get(':id')
	@ApiOkResponse({ type: ProductEntity })
	findOne(@Param('id', ParseIntPipe) id: number) {
		return this.productsService.findOne(+id);
	}

	@Patch(':id')
	@ApiOkResponse({ type: ProductEntity })
	update(@Param('id') id: string, @Body() updateProductDto: UpdateProductDto) {
		return this.productsService.update(+id, updateProductDto);
	}

	@Delete(':id')
	@ApiOkResponse({ type: ProductEntity })
	remove(@Param('id') id: string) {
		return this.productsService.remove(+id);
	}
}
