import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ProtectedMutation } from '../../common/audit/protected-mutation.decorator';
import { VersionService } from './version.service';
import { CreateVersionDto } from './dto/create-version.dto';
import { UpdateVersionDto } from './dto/update-version.dto';

@Controller('products/:productId')
export class VersionController {
  constructor(private readonly versionService: VersionService) {}

  @Post('versions')
  @ProtectedMutation('version:create', { productIdParam: 'productId' }, {
    action: 'version.create',
    entityType: 'version',
    productIdParam: 'productId',
  })
  create(@Param('productId') productId: string, @Body() dto: CreateVersionDto) {
    return this.versionService.create(productId, dto);
  }

  @Get('versions')
  findAll(@Param('productId') productId: string) {
    return this.versionService.findAll(productId);
  }

  @Post('projects/:projectId/versions')
  @ProtectedMutation('version:create', { productIdParam: 'productId', projectIdParam: 'projectId' }, {
    action: 'version.create',
    entityType: 'version',
    productIdParam: 'productId',
    projectIdParam: 'projectId',
  })
  createForProject(
    @Param('productId') productId: string,
    @Param('projectId') projectId: string,
    @Body() dto: CreateVersionDto,
  ) {
    return this.versionService.create(productId, dto, projectId);
  }

  @Get('projects/:projectId/versions')
  findAllForProject(@Param('productId') productId: string, @Param('projectId') projectId: string) {
    return this.versionService.findAll(productId, projectId);
  }

  @Patch('versions/:versionId')
  @ProtectedMutation('version:edit', { productIdParam: 'productId', versionIdParam: 'versionId' }, {
    action: 'version.update',
    entityType: 'version',
    entityIdParam: 'versionId',
    productIdParam: 'productId',
    versionIdParam: 'versionId',
  })
  update(
    @Param('productId') productId: string,
    @Param('versionId') versionId: string,
    @Body() dto: UpdateVersionDto,
  ) {
    return this.versionService.update(productId, versionId, dto);
  }

  @Delete('versions/:versionId')
  @ProtectedMutation('version:delete', { productIdParam: 'productId', versionIdParam: 'versionId' }, {
    action: 'version.delete',
    entityType: 'version',
    entityIdParam: 'versionId',
    productIdParam: 'productId',
    versionIdParam: 'versionId',
  })
  remove(@Param('productId') productId: string, @Param('versionId') versionId: string) {
    return this.versionService.remove(productId, versionId);
  }
}
