import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query, Res } from '@nestjs/common';
import { FilterTestResultService } from './filter-test-result.service';
import { CreateFilterTestResultDto, CreateFilterTestResultDtoV2 } from './dto/create-filter-test-result.dto';
import { ApiBearerAuth, ApiBody, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { RolesGuard } from 'src/guards/roles.guard';
import { Roles } from 'src/common/decorators/roles.decorator';
import { USER_ROLE } from '../users/users.constant';
import { ResponseMessage } from 'src/common/decorators/response_message.decorator';
import { PaginatedQueryDto } from 'src/common/builder/QueryBuilder.dto';
import { Response } from 'express';

@ApiTags('Filter Test Result Endpoints')
@Controller('filter-test-result')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
export class FilterTestResultController {
  constructor(private readonly filterTestResultService: FilterTestResultService) { }

  @Post('/run-test')
  @Roles(USER_ROLE.super_admin)
  @ResponseMessage('Successfully created a new run test')
  @ApiBody({ type: CreateFilterTestResultDtoV2 })
  async create(
    @Body() body: CreateFilterTestResultDtoV2,
    @Res() res: Response
  ) {
    const result = await this.filterTestResultService.runFilterTestV2(body, res);
    return result;
  }


  @Post('/rerun-test/:testId')
  @Roles(USER_ROLE.super_admin)
  @ResponseMessage('Successfully created a new run test')
  @ApiBody({ type: CreateFilterTestResultDtoV2 })
  async rerunTest(
    @Param('testId') testId: string,
    @Res() res: Response
  ) {
    const result = await this.filterTestResultService.rerunFilterTest(testId, res);
    return result;
  }





  // @Post('/run-test')
  // @Roles(USER_ROLE.super_admin)
  // @ResponseMessage('Successfully created a new run test')
  // @ApiBody({ type: CreateFilterTestResultDto })
  // async create(
  //   @Body() body: CreateFilterTestResultDto,
  //   @Res() res: Response
  // ) {
  //   const result = await this.filterTestResultService.runFilterTest(body, res);
  //   return result;
  // }




  @Get('/get-all-results')
  @Roles(USER_ROLE.super_admin)
  @ResponseMessage('Successfully fetched all test results')
  @ApiQuery({ type: PaginatedQueryDto })
  async findAll(
    @Query() query: Record<string, unknown>,
  ) {
    const result = await this.filterTestResultService.getFilterResults(query);
    return result;
  }



  @Get('/get-result/:id')
  @Roles(USER_ROLE.super_admin)
  @ResponseMessage('Successfully fetched test result')
  @ApiParam({ name: 'id', type: String })
  async findOne(@Param('id') id: string) {
    const result = await this.filterTestResultService.getFilterResultById(id);
    return result;
  }



  @Delete('/delete-result/:id')
  @Roles(USER_ROLE.super_admin)
  @ResponseMessage('Successfully deleted test result')
  @ApiParam({ name: 'id', type: String })
  async remove(@Param('id') id: string) {
    const result = await this.filterTestResultService.deleteFilterResult(id);
    return result;
  }

}
