import { Injectable, Logger } from '@nestjs/common';

/**
 * OCR provider interface. Real impl would call AWS Rekognition, Google Vision,
 * Azure or an on-prem ALPR system (e.g. OpenALPR). Returns plate candidates
 * with confidence; service uses the top hit above threshold.
 */
export interface PlateRecognitionProvider {
  readonly code: string;
  recognize(imageBase64: string): Promise<{ plate: string; confidence: number }[]>;
}

@Injectable()
export class StubPlateRecognitionProvider implements PlateRecognitionProvider {
  readonly code = 'STUB';
  private readonly logger = new Logger('StubOCR');

  async recognize(imageBase64: string) {
    if (process.env.ENABLE_STUB_PROVIDERS !== 'true') {
      this.logger.warn('Plate OCR provider is not configured');
      return [];
    }
    // Stub: in tests we encode the plate string as base64 directly (prefix "PLATE:").
    try {
      const decoded = Buffer.from(imageBase64, 'base64').toString('utf8');
      if (decoded.startsWith('PLATE:')) {
        const plate = decoded.slice(6).toUpperCase();
        this.logger.log(`[STUB OCR] recognized plate=${plate}`);
        return [{ plate, confidence: 0.95 }];
      }
    } catch {}
    return [];
  }
}
