import { Controller, Get, Post, Body, Patch, Param, Delete, Query, DefaultValuePipe, ParseIntPipe } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { ApiCreatedResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { CategoryEntity } from './entities/category.entity';

@Controller('categories')
@ApiTags('categories')
export class CategoriesController {
	constructor(private readonly categoriesService: CategoriesService) {}

	@Post()
	@ApiCreatedResponse({ type: CategoryEntity })
	create(@Body() createCategoryDto: CreateCategoryDto) {
		return this.categoriesService.create(createCategoryDto);
	}

	@Get()
	@ApiOkResponse({ type: CategoryEntity, 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.categoriesService.findAll((page - 1) * limit, limit, sortBy, sortOrder, search);
	}

	@Get(':id')
	@ApiOkResponse({ type: CategoryEntity })
	findOne(@Param('id') id: string) {
		return this.categoriesService.findOne(+id);
	}

	@Patch(':id')
	@ApiOkResponse({ type: CategoryEntity })
	update(@Param('id') id: string, @Body() updateCategoryDto: UpdateCategoryDto) {
		return this.categoriesService.update(+id, updateCategoryDto);
	}

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