import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import {
  ReferenceValueCreateSchema,
  ReferenceValueUpdateSchema,
  type ReferenceValueCreate,
  type ReferenceValueUpdate,
} from '@siop/shared';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { ReferenceValuesService } from './reference-values.service';

@Controller('reference-values')
export class ReferenceValuesController {
  constructor(private readonly referenceValues: ReferenceValuesService) {}

  /** Lu par le formulaire de bilan — authentification seule. */
  @Get()
  list() {
    return this.referenceValues.list();
  }

  @Post()
  @RequirePermission('SETTINGS', 'create')
  create(
    @Body(new ZodValidationPipe(ReferenceValueCreateSchema)) body: ReferenceValueCreate,
  ) {
    return this.referenceValues.create(body);
  }

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