import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ProtectedMutation } from '../../common/audit/protected-mutation.decorator';
import { CreateDevTaskDto } from './dto/create-dev-task.dto';
import { UpdateDevTaskDto } from './dto/update-dev-task.dto';
import { DevTaskService } from './dev-task.service';

@Controller('versions/:versionId/dev-tasks')
export class DevTaskController {
  constructor(private readonly devTaskService: DevTaskService) {}

  @Post()
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.create',
    entityType: 'dev_task',
    versionIdParam: 'versionId',
  })
  create(@Param('versionId') versionId: string, @Body() dto: CreateDevTaskDto) {
    return this.devTaskService.create(versionId, dto);
  }

  @Get()
  findAll(@Param('versionId') versionId: string) {
    return this.devTaskService.findAll(versionId);
  }

  @Patch(':id')
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.update',
    entityType: 'dev_task',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  update(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateDevTaskDto) {
    return this.devTaskService.update(versionId, id, dto);
  }

  @Patch(':id/status')
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.status',
    entityType: 'dev_task',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  updateStatus(
    @Param('versionId') versionId: string,
    @Param('id') id: string,
    @Body('status') status: string,
  ) {
    return this.devTaskService.updateStatus(versionId, id, status);
  }

  @Patch(':id/block')
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.block',
    entityType: 'dev_task',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  setBlocked(
    @Param('versionId') versionId: string,
    @Param('id') id: string,
    @Body('blocked') blocked: boolean,
    @Body('reason') reason?: string,
  ) {
    return this.devTaskService.setBlocked(versionId, id, blocked, reason);
  }

  @Patch(':id/transfer')
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.transfer',
    entityType: 'dev_task',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  transfer(
    @Param('versionId') versionId: string,
    @Param('id') id: string,
    @Body('assigneeId') assigneeId: string,
  ) {
    return this.devTaskService.transfer(versionId, id, assigneeId);
  }

  @Delete(':id')
  @ProtectedMutation('version.devtask:manage', { versionIdParam: 'versionId' }, {
    action: 'dev_task.delete',
    entityType: 'dev_task',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  remove(@Param('versionId') versionId: string, @Param('id') id: string) {
    return this.devTaskService.remove(versionId, id);
  }
}
