import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ProtectedMutation } from '../../common/audit/protected-mutation.decorator';
import { ProjectService } from './project.service';
import { CreateProjectDto } from './dto/create-project.dto';
import { UpdateProjectDto } from './dto/update-project.dto';

@Controller('products/:productId/projects')
export class ProjectController {
  constructor(private readonly projectService: ProjectService) {}

  @Post()
  @ProtectedMutation('project:create', { productIdParam: 'productId' }, {
    action: 'project.create',
    entityType: 'project',
    productIdParam: 'productId',
  })
  create(@Param('productId') productId: string, @Body() dto: CreateProjectDto) {
    return this.projectService.create(productId, dto);
  }

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

  @Patch(':projectId')
  @ProtectedMutation('project:edit', { productIdParam: 'productId', projectIdParam: 'projectId' }, {
    action: 'project.update',
    entityType: 'project',
    entityIdParam: 'projectId',
    productIdParam: 'productId',
    projectIdParam: 'projectId',
  })
  update(
    @Param('productId') productId: string,
    @Param('projectId') projectId: string,
    @Body() dto: UpdateProjectDto,
  ) {
    return this.projectService.update(productId, projectId, dto);
  }

  @Delete(':projectId')
  @ProtectedMutation('project:delete', { productIdParam: 'productId', projectIdParam: 'projectId' }, {
    action: 'project.delete',
    entityType: 'project',
    entityIdParam: 'projectId',
    productIdParam: 'productId',
    projectIdParam: 'projectId',
  })
  remove(@Param('productId') productId: string, @Param('projectId') projectId: string) {
    return this.projectService.remove(productId, projectId);
  }
}
