import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import {
  PartCreateSchema,
  PartUpdateSchema,
  StockMovementCreateSchema,
  type PartCreate,
  type PartUpdate,
  type StockMovementCreate,
} from '@siop/shared';
import {
  AuthenticatedUser,
  CurrentUser,
} from '../auth/current-user.decorator';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { PartsService } from './parts.service';

@Controller('parts')
export class PartsController {
  constructor(private readonly parts: PartsService) {}

  @Get()
  @RequirePermission('PARTS', 'view')
  list() {
    return this.parts.list();
  }

  @Get(':id')
  @RequirePermission('PARTS', 'view')
  get(@Param('id', ParseUUIDPipe) id: string) {
    return this.parts.get(id);
  }

  @Post()
  @RequirePermission('PARTS', 'create')
  create(@Body(new ZodValidationPipe(PartCreateSchema)) body: PartCreate) {
    return this.parts.create(body);
  }

  @Patch(':id')
  @RequirePermission('PARTS', 'edit')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(PartUpdateSchema)) body: PartUpdate,
  ) {
    return this.parts.update(id, body);
  }

  @Post(':id/movements')
  @RequirePermission('PARTS', 'edit')
  addMovement(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(StockMovementCreateSchema)) body: StockMovementCreate,
    @CurrentUser() user: AuthenticatedUser,
  ) {
    return this.parts.addMovement(id, body, user);
  }
}
