import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ProtectedMutation } from '../../common/audit/protected-mutation.decorator';
import { CreateTestCaseDto } from './dto/create-test-case.dto';
import { UpdateTestCaseDto } from './dto/update-test-case.dto';
import { TestCaseService } from './test-case.service';

@Controller('versions/:versionId/test-cases')
export class TestCaseController {
  constructor(private readonly testCaseService: TestCaseService) {}

  @Post()
  @ProtectedMutation('version.testcase:manage', { versionIdParam: 'versionId' }, {
    action: 'test_case.create',
    entityType: 'test_case',
    versionIdParam: 'versionId',
  })
  create(@Param('versionId') versionId: string, @Body() dto: CreateTestCaseDto) {
    return this.testCaseService.create(versionId, dto);
  }

  @Post('batch')
  @ProtectedMutation('version.testcase:manage', { versionIdParam: 'versionId' }, {
    action: 'test_case.batch_create',
    entityType: 'test_case',
    versionIdParam: 'versionId',
  })
  createMany(@Param('versionId') versionId: string, @Body('items') items: CreateTestCaseDto[]) {
    return this.testCaseService.createMany(versionId, items ?? []);
  }

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

  @Patch(':id')
  @ProtectedMutation('version.testcase:manage', { versionIdParam: 'versionId' }, {
    action: 'test_case.update',
    entityType: 'test_case',
    entityIdParam: 'id',
    versionIdParam: 'versionId',
  })
  update(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateTestCaseDto) {
    return this.testCaseService.update(versionId, id, dto);
  }

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

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