import { Controller, Post, Body, HttpException, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { OwaspService } from './owasp.service';
import { RunOwaspScanDto } from './dto/run-owasp-scan.dto';
import { CombinedOwaspResults } from './interface/owasp-results.interface';

@ApiTags('OWASP Combined Scanner')
@Controller('api/owasp/scanAll')
export class OwaspController {
  constructor(private readonly owaspService: OwaspService) {}

  @Post('scan')
  @ApiOperation({
    summary: 'Run All OWASP Security Scans',
    description: 'Runs a combined scan for Broken Access Control and Cryptographic Failures against a target URL.',
  })
  @ApiBody({ type: RunOwaspScanDto })
  @ApiResponse({
    status: 201,
    description: 'Returns a combined report of all security test results.',
  })
  @ApiResponse({ status: 400, description: 'Invalid or missing URL.' })
  @ApiResponse({ status: 500, description: 'An error occurred during the scan.' })
  async runAllScans(@Body() runOwaspScanDto: RunOwaspScanDto): Promise<CombinedOwaspResults> {
    try {
      return await this.owaspService.runAllScans(runOwaspScanDto.url);
    } catch (error) {
      throw new HttpException(
        `Failed to run the combined scan: ${error.message}`,
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}