import { Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { Roles } from '../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { CatalogService } from './catalog.service';
import { UpsertCategoryDto } from './dto/upsert-category.dto';
import { UpsertProductDto } from './dto/upsert-product.dto';

@Controller('catalog')
@UseGuards(JwtAuthGuard, RolesGuard)
export class CatalogController {
  constructor(private readonly catalogService: CatalogService) {}

  @Get('categories')
  @Roles('RESTAURANT', 'ADMIN', 'SUPER_ADMIN')
  listCategories(@Req() req: { user: { sub: string; role: string } }) {
    return this.catalogService.listCategories(req.user as never);
  }

  @Post('categories')
  @Roles('RESTAURANT')
  createCategory(@Req() req: { user: { sub: string; role: string } }, @Body() body: UpsertCategoryDto) {
    return this.catalogService.createCategory(req.user as never, body);
  }

  @Patch('categories/:id')
  @Roles('RESTAURANT')
  updateCategory(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string, @Body() body: Partial<UpsertCategoryDto>) {
    return this.catalogService.updateCategory(req.user as never, id, body);
  }

  @Delete('categories/:id')
  @Roles('RESTAURANT')
  archiveCategory(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.catalogService.archiveCategory(req.user as never, id);
  }

  @Get('products')
  @Roles('RESTAURANT', 'ADMIN', 'SUPER_ADMIN')
  listProducts(@Req() req: { user: { sub: string; role: string } }) {
    return this.catalogService.listProducts(req.user as never);
  }

  @Post('products')
  @Roles('RESTAURANT')
  createProduct(@Req() req: { user: { sub: string; role: string } }, @Body() body: UpsertProductDto) {
    return this.catalogService.createProduct(req.user as never, body);
  }

  @Patch('products/:id')
  @Roles('RESTAURANT')
  updateProduct(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string, @Body() body: Partial<UpsertProductDto>) {
    return this.catalogService.updateProduct(req.user as never, id, body);
  }

  @Delete('products/:id')
  @Roles('RESTAURANT')
  archiveProduct(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.catalogService.archiveProduct(req.user as never, id);
  }
}
