import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import {
  CategoryCreateSchema,
  CategoryUpdateSchema,
  type CategoryCreate,
  type CategoryUpdate,
} from '@siop/shared';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { CategoriesService } from './categories.service';

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

  /** Donnée de référence lue par les formulaires — authentification seule. */
  @Get()
  list() {
    return this.categoriesService.list();
  }

  @Post()
  @RequirePermission('SETTINGS', 'create')
  create(@Body(new ZodValidationPipe(CategoryCreateSchema)) body: CategoryCreate) {
    return this.categoriesService.create(body);
  }

  @Patch(':id')
  @RequirePermission('SETTINGS', 'edit')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(CategoryUpdateSchema)) body: CategoryUpdate,
  ) {
    return this.categoriesService.update(id, body);
  }
}
