import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import {
  LocationCreateSchema,
  LocationUpdateSchema,
  type LocationCreate,
  type LocationUpdate,
} from '@siop/shared';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { LocationsService } from './locations.service';

@Controller('locations')
export class LocationsController {
  constructor(private readonly locationsService: LocationsService) {}

  @Get()
  @RequirePermission('LOCATIONS', 'view')
  list() {
    return this.locationsService.list();
  }

  @Post()
  @RequirePermission('LOCATIONS', 'create')
  create(@Body(new ZodValidationPipe(LocationCreateSchema)) body: LocationCreate) {
    return this.locationsService.create(body);
  }

  @Patch(':id')
  @RequirePermission('LOCATIONS', 'edit')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(LocationUpdateSchema)) body: LocationUpdate,
  ) {
    return this.locationsService.update(id, body);
  }
}
