import {
    Controller,
    Post,
    UseGuards,
    UseInterceptors,
    UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { OcrService } from './ocr.service';
import { I18nService } from '../i18n/i18n.service';

@Controller('ocr')
@UseGuards(CombinedAuthGuard)
@UseGuards(CombinedAuthGuard)
export class OcrController {
    constructor(
        private readonly ocrService: OcrService,
        private readonly i18n: I18nService
    ) { }

    @Post('recognize')
    @UseInterceptors(FileInterceptor('image'))
    async recognizeText(@UploadedFile() image: Express.Multer.File) {
        console.log('OCR recognition endpoint called');
        if (!image) {
            console.error('No image uploaded');
            throw new Error(this.i18n.getMessage('noImageUploaded'));
        }
        console.log(`Received image. Size: ${image.size} bytes`);
        const text = await this.ocrService.extractTextFromImage(image.buffer);
        console.log(`OCR extraction completed. Text length: ${text.length}`);
        return { text };
    }
}
