import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ProtectedMutation } from '../../common/audit/protected-mutation.decorator';
import { CreateBugDto } from './dto/create-bug.dto';
import { UpdateBugDto } from './dto/update-bug.dto';
import { BugService } from './bug.service';

@Controller('versions/:versionId/bugs')
export class BugController {
  constructor(private readonly bugService: BugService) {}

  @Post()
  @ProtectedMutation('version.bug:create', { versionIdParam: 'versionId' }, {
    action: 'bug.create',
    entityType: 'bug',
    versionIdParam: 'versionId',
  })
  create(@Param('versionId') versionId: string, @Body() dto: CreateBugDto) {
    return this.bugService.create(versionId, dto);
  }

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

  @Patch(':id')
  @ProtectedMutation('version.bug:edit', { versionIdParam: 'versionId' }, {
    action: 'bug.update',
    entityType: 'bug',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  update(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateBugDto) {
    return this.bugService.update(versionId, id, dto);
  }

  @Patch(':id/status')
  @ProtectedMutation('version.bug:edit', { versionIdParam: 'versionId' }, {
    action: 'bug.status',
    entityType: 'bug',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  updateStatus(
    @Param('versionId') versionId: string,
    @Param('id') id: string,
    @Body('status') status: string,
    @Body() body: { operator?: string; resolution?: string },
  ) {
    return this.bugService.updateStatus(versionId, id, status, body);
  }

  @Patch(':id/transfer')
  @ProtectedMutation('version.bug:edit', { versionIdParam: 'versionId' }, {
    action: 'bug.transfer',
    entityType: 'bug',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  transfer(
    @Param('versionId') versionId: string,
    @Param('id') id: string,
    @Body('assigneeId') assigneeId: string,
    @Body('operator') operator?: string,
  ) {
    return this.bugService.transfer(versionId, id, assigneeId, operator);
  }

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