import { Controller, Post, Body, UploadedFile, UseInterceptors, BadRequestException, HttpStatus } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AiScannerService } from './ai-scanner.service';
import {
  AiAnalysisResult,
  AnalyzeTextDto,
  FileAnalysisResult,
  SentenceAnalysisResponse,
  AiNepaliAnalysisResult,
  NepaliSentenceAnalysisResult,
  NepaliSentenceAnalysisResponse,
  NepaliFileAnalysisResult,
  NepaliFileSentenceAnalysisResult,
  ImageAiDetector,
  ElaDetect,
  FFTDetect,
  MetaDetect,
  ImageForensis,
} from './dto/analyze-text.dto';
import { ApiTags, ApiConsumes, ApiBody, ApiOperation, ApiResponse, ApiProperty } from '@nestjs/swagger';
import { RateLimit } from 'common/decorators/rate-limit.decorator';
import { Roles } from 'common/decorators/role.decorator';
import { UserRole } from 'common/enum/UserRole.enum';

@ApiTags('AI Scanner')
@Controller({ path: 'ai-scanner', version: '1' })
export class AiScannerController {
  constructor(private readonly aiScannerService: AiScannerService) {}

  @Post('analyze')
  @Roles(UserRole.CLIENT, UserRole.ADMIN, UserRole.SUPER_ADMIN, UserRole.USER)
  // @Permissions(FeatureEnum.ANALYZE)
  @ApiOperation({
    summary: 'Analyze text for AI-generated content',
    description: 'Analyzes the provided text to determine if it was generated by AI. Returns a probability score and classification.',
  })
  @ApiBody({
    description: 'Text content to be analyzed',
    type: AnalyzeTextDto,
    examples: {
      example1: {
        summary: 'Sample text analysis',
        description: 'Example of text that might be AI-generated',
        value: {
          text: 'Artificial intelligence has revolutionized numerous industries by providing sophisticated solutions to complex problems. Machine learning algorithms can process vast amounts of data with unprecedented efficiency, enabling organizations to make data-driven decisions with remarkable accuracy.',
        },
      },
      example2: {
        summary: 'Academic text',
        description: 'Example of academic or research text',
        value: {
          text: 'The methodology employed in this study utilized a mixed-methods approach, combining quantitative analysis with qualitative insights to provide a comprehensive understanding of the research question.',
        },
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Text analysis completed successfully',
    type: AiAnalysisResult,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - Invalid input data',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: {
          type: 'array',
          items: { type: 'string' },
          example: ['text should not be empty', 'text must be shorter than or equal to 10000 characters'],
        },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async analyzeText(@Body() analyzeTextDto: AnalyzeTextDto): Promise<AiAnalysisResult> {
    return this.aiScannerService.analyzeText(analyzeTextDto.text);
  }

  @Post('analyze-sentences')
  @Roles(UserRole.CLIENT, UserRole.ADMIN, UserRole.SUPER_ADMIN, UserRole.USER)
  // @Permissions(FeatureEnum.ANALYZE_SENTENCE)
  @ApiOperation({
    summary: 'Analyze text with sentence-level breakdown',
    description: 'Analyzes the provided text and returns both overall analysis and individual sentence-level analysis results.',
  })
  @ApiBody({
    description: 'Text content to be analyzed with sentence-level detail',
    type: AnalyzeTextDto,
    examples: {
      example1: {
        summary: 'Multi-sentence text',
        description: 'Example with multiple sentences for detailed analysis',
        value: {
          text: 'The rapid advancement of artificial intelligence has transformed modern society. AI systems now assist in medical diagnosis, financial analysis, and creative content generation. However, concerns about job displacement and ethical implications continue to emerge. Researchers are working to address these challenges through responsible AI development.',
        },
      },
      example2: {
        summary: 'Short paragraph',
        description: 'Brief text for sentence-by-sentence analysis',
        value: {
          text: 'Climate change represents one of the most pressing challenges of our time. Scientists worldwide are studying its effects on ecosystems and human societies. Immediate action is required to mitigate its impact.',
        },
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Sentence-level analysis completed successfully',
    type: SentenceAnalysisResponse,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - Invalid input data',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: {
          type: 'array',
          items: { type: 'string' },
          example: ['text should not be empty', 'text must be shorter than or equal to 10000 characters'],
        },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async analyzeSentences(@Body() analyzeTextDto: AnalyzeTextDto): Promise<SentenceAnalysisResponse> {
    return this.aiScannerService.analyzeSentences(analyzeTextDto.text);
  }

  @Post('analyze-file')
  @UseInterceptors(FileInterceptor('file'))
  @Roles(UserRole.CLIENT, UserRole.ADMIN, UserRole.SUPER_ADMIN, UserRole.USER)
  // @Permissions(FeatureEnum.ANALYZE_FILE)
  @ApiOperation({
    summary: 'Analyze uploaded file for AI-generated content',
    description: 'Uploads and analyzes a file (PDF, DOCX, or TXT) to determine if the content was generated by AI.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'File to be analyzed (PDF, DOCX, or TXT)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'The file to analyze (max size: 10MB, supported formats: PDF, DOCX, TXT)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'File analysis completed successfully',
    type: FileAnalysisResult,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid file type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid file type. Only PDF, DOCX, and TXT files are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async analyzeFile(@UploadedFile() file: Express.Multer.File): Promise<FileAnalysisResult> {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const allowedMimeTypes = ['application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type. Only PDF, DOCX, and TXT files are allowed.');
    }

    return this.aiScannerService.analyzeFile(file);
  }

  @Post('analyze-file-sentences')
  @UseInterceptors(FileInterceptor('file'))
  @Roles(UserRole.CLIENT, UserRole.ADMIN, UserRole.SUPER_ADMIN, UserRole.USER)
  // @Permissions(FeatureEnum.ANALYZE_FILE_SENTENCE)
  @ApiOperation({
    summary: 'Analyze uploaded file with sentence-level breakdown',
    description: 'Uploads and analyzes a file (PDF, DOCX, or TXT) with detailed sentence-level analysis for AI-generated content detection.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'File to be analyzed with sentence-level detail (PDF, DOCX, or TXT)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'The file to analyze (max size: 10MB, supported formats: PDF, DOCX, TXT)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'File sentence-level analysis completed successfully',
    schema: {
      allOf: [
        { $ref: '#/components/schemas/SentenceAnalysisResponse' },
        {
          type: 'object',
          properties: {
            filename: {
              type: 'string',
              description: 'Original filename of the uploaded file',
              example: 'document.pdf',
            },
            fileType: {
              type: 'string',
              description: 'MIME type of the analyzed file',
              example: 'application/pdf',
            },
          },
        },
      ],
    },
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid file type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid file type. Only PDF, DOCX, and TXT files are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async analyzeFileSentences(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const allowedMimeTypes = ['application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type. Only PDF, DOCX, and TXT files are allowed.');
    }

    return this.aiScannerService.analyzeFileSentences(file);
  }
  // Nepali -text processing
  @Post('NP/analyze')
  @ApiOperation({
    summary: 'Analyze Nepali text for AI-generated content',
    description: 'Analyzes the provided text to determine if it was generated by AI. Returns a probability score and classification.',
  })
  @ApiBody({
    description: 'Text content to be analyzed',
    type: AnalyzeTextDto,
    examples: {
      example1: {
        summary: 'Sample text analysis',
        description: 'Example of text that might be AI-generated',
        value: {
          text: 'एआईले उत्पादन गर्ने नेपाली सामग्री तीव्र रूपमा बढ्दैछ, जसले समाचार, निबन्ध र सामाजिक सञ्जातिक गहिराइ र सन्दर्भगत सूक्ष्मता छुटाउँछ।',
        },
      },
      example2: {
        summary: 'Academic text',
        description: 'Example of academic or research text',
        value: {
          text: 'कम विकसित मुलुकको सूचीबाट नेपाल माथि उठ्न लागिरहेका बेला यो रणनीतिले देशलाई थप दिगो र समावेशी आर्थिक वृद्धिको दिशामा लैजानेछ...',
        },
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Text analysis completed successfully',
    type: AiNepaliAnalysisResult,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - Invalid input data',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: {
          type: 'array',
          items: { type: 'string' },
          example: ['text should not be empty', 'text must be shorter than or equal to 10000 characters'],
        },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async nepalianalyze(@Body() analyzeTextDto: AnalyzeTextDto): Promise<AiNepaliAnalysisResult> {
    return this.aiScannerService.nepalianalyze(analyzeTextDto.text);
  }
  @Post('NP/analyze-sentences')
  @ApiOperation({
    summary: 'Analyze text with sentence-level breakdown',
    description: 'Analyzes the provided text and returns both overall analysis and individual sentence-level analysis results.',
  })
  @ApiBody({
    description: 'Text content to be analyzed with sentence-level detail',
    type: AnalyzeTextDto,
    examples: {
      example1: {
        summary: 'Multi-sentence paragraph',
        description: 'Example containing several meaningful Nepali sentences.',
        value: {
          text: 'नेपाल सुन्दर हिमाली देश हो जहाँ हिमाल, पहाड र तराईका भित्री भूभागहरू छन्। यहाँका मानिसहरू माया र सम्मानले भरिपूर्ण छन् र आफ्नो सांस्कृतिक र परम्परालाई ठूलो गर्वले जगेर्ना गर्छन्। नेपाली भाषा मधुरो र भावुक छ, जसले हाम्रो पहिचानलाई अझ बलियो बनाउँछ।',
        },
      },
      example2: {
        summary: 'Short paragraph with nature and culture',
        description: 'Another example paragraph with different sentence content.',
        value: {
          text: 'नेपाल सुन्दर हिमाली देश हो जहाँ हिमाल, पहाड र तराईका भित्री भूभागहरू छन्। यहाँका मानिसहरू माया र सम्मानले भरिपूर्ण छन्। नेपाली भाषा मधुरो र भावुक छ।',
        },
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Sentence-level analysis completed successfully',
    type: NepaliSentenceAnalysisResponse,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - Invalid input data',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: {
          type: 'array',
          items: { type: 'string' },
          example: ['text should not be empty', 'text must be shorter than or equal to 10000 characters'],
        },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  })
  async nepalianalyzeSentences(@Body() analyzeTextDto: AnalyzeTextDto): Promise<NepaliSentenceAnalysisResponse> {
    return this.aiScannerService.nepalianalyzeSentences(analyzeTextDto.text);
  }
  // Nepali File Analysis Endpoint
  @Post('NP/analyze-file')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Analyze Nepali file content for AI-generation',
    description: 'Uploads a file (PDF, DOCX, or TXT) and analyzes its content to detect if it was AI-generated.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Upload a file (Max: 10MB, Formats: PDF, DOCX, TXT)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'The file to analyze',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Analysis successful',
    type: NepaliFileAnalysisResult,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - Invalid or missing file',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: {
          type: 'string',
          example: 'Invalid file type. Only PDF, DOCX, and TXT files are allowed.',
        },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  })
  async analyzeNepaliFile(@UploadedFile() file: Express.Multer.File): Promise<NepaliFileAnalysisResult> {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const supportedTypes = ['application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain'];

    if (!supportedTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type. Only PDF, DOCX, and TXT files are allowed.');
    }

    return this.aiScannerService.nepalianalyzeFile(file);
  }

  @Post('/NP/analysis-file-sentences')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Analyze uploaded file with sentence-level breakdown',
    description:
      'Uploads and analyzes a file (PDF, DOCX, or TXT) with detailed sentence-level analysis for AI-generated in Nepali language content detection.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'File to be analyzed with sentence-level detail (PDF, DOCX, or TXT)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'The file to analyze (max size: 10MB, supported formats: PDF, DOCX, TXT)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'File sentence-level analysis completed successfully',
    type: NepaliSentenceAnalysisResponse,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid file type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid file type. Only PDF, DOCX, and TXT files are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async nepalianalyzeFileSentences(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const allowedMimeTypes = ['application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type. Only PDF, DOCX, and TXT files are allowed.');
    }

    return this.aiScannerService.nepalianalyzeFileSentences(file);
  }

  @Post('/Image/AI-Image-Detector')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Analyze uploaded image for AI-generation detection',
    description: 'Uploads and analyzes an image (.png, .jpg, .jpeg) to detect AI-generated content in the image.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Image file to be analyzed (PNG, JPG, JPEG)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Image file (max size: 10MB, formats: PNG, JPG, JPEG)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Image analysis completed successfully',
    type: ImageAiDetector, // <- Make sure this DTO exists
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid image type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid image type. Only PNG, JPG, and JPEG are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async aiimageanalyzer(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid image type. Only PNG, JPG, and JPEG are allowed.');
    }

    return this.aiScannerService.aiimageanalyze(file); // Make sure this handles image processing
  }

  @Post('/detect/ela')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Analyze uploaded image for editing in it',
    description: 'Uploads and analyzes an image (.png, .jpg, .jpeg) to detect edit  in the image.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Image file to be analyzed (PNG, JPG, JPEG)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Image file (max size: 10MB, formats: PNG, JPG, JPEG)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Image analysis completed successfully',
    type: ElaDetect, // <- Make sure this DTO exists
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid image type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid image type. Only PNG, JPG, and JPEG are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async eladetector(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }

    const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid image type. Only PNG, JPG, and JPEG are allowed.');
    }

    return this.aiScannerService.elaimagedetectorsystem(file); // Make sure this handles image processing
  }

  // fft

  @Post('/detect/fft')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Analyze uploaded image for editing in it',
    description: 'Uploads and analyzes an image (.png, .jpg, .jpeg) to detect edit  in the image.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Image file to be analyzed (PNG, JPG, JPEG)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Image file (max size: 10MB, formats: PNG, JPG, JPEG)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Image analysis completed successfully',
    type: FFTDetect, // <- Make sure this DTO exists
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid image type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid image type. Only PNG, JPG, and JPEG are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async fftdetector(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }
    const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid image type. Only PNG, JPG, and JPEG are allowed.');
    }

    return this.aiScannerService.fftimagedetectorsystem(file);
  }

  // meta
  @Post('/detect/meta')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Detect AI-generated image using metadata analysis',
    description:
      'Uploads an image (.png, .jpg, .jpeg) and analyzes its embedded metadata (EXIF, software tags, etc.) to determine whether it was likely generated by an AI model or captured by a real device.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Image file to be analyzed (PNG, JPG, JPEG)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Image file (max size: 10MB, formats: PNG, JPG, JPEG)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Image analysis completed successfully',
    type: MetaDetect, // <- Make sure this DTO exists
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid image type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid image type. Only PNG, JPG, and JPEG are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async metadetector(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }
    const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid image type. Only PNG, JPG, and JPEG are allowed.');
    }

    return this.aiScannerService.metaimagedetectorsystem(file);
  }

  @Post('/detect/image-forensics')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Comprehensive forensic analysis to detect edited images',
    description: 'Uploads an image and analyzes it using ELA, FFT, and metadata to determine if it is manipulated.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Image file to be analyzed (PNG, JPG, JPEG)',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Image file (max size: 10MB, formats: PNG, JPG, JPEG)',
        },
      },
      required: ['file'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Image analysis completed successfully',
    type: ImageForensis, // <- Make sure this DTO exists
  })
  @ApiResponse({
    status: 400,
    description: 'Bad Request - No file uploaded or invalid image type',
    schema: {
      type: 'object',
      properties: {
        statusCode: { type: 'number', example: 400 },
        message: { type: 'string', example: 'Invalid image type. Only PNG, JPG, and JPEG are allowed.' },
        error: { type: 'string', example: 'Bad Request' },
      },
    },
  })
  @RateLimit({
    message: 'Too many requests, please try again later',
    statusCode: HttpStatus.TOO_MANY_REQUESTS,
    headers: true,
    max: 20,
    windowMs: 60 * 1000,
  }) // 20 requests per minute
  async image_forensics(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException('No file uploaded');
    }
    const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];

    if (!allowedMimeTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid image type. Only PNG, JPG, and JPEG are allowed.');
    }

    return this.aiScannerService.imageforensicssystem(file);
  }
}
// all 3 at once meta ela and fft
