import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:tane/services/ocr/tesseract_label_extractor.dart';

/// Real, on-device OCR validation against actual packet photos — the "does
/// Tesseract really read my packets" gate from the plan. Runs the native engine,
/// so it must run on a device/emulator, NOT under `flutter test` on the host:
///
///   `flutter test integration_test/ocr_label_test.dart -d <android-device>`
///
/// Drop the photos to check into `assets/ocr_fixtures/` (declared in pubspec)
/// with the file names below. Each test prints what Tesseract actually read so
/// you can see and tune it, then asserts the expected variety word is present.
void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  const extractor = TesseractLabelExtractor();

  Future<void> reportRead(String asset) async {
    final data = await rootBundle.load('assets/ocr_fixtures/$asset');
    final result = await extractor.suggestLabel(data.buffer.asUint8List());
    // ignore: avoid_print
    print('OCR[$asset] => "$result"');
    // Best-effort: a usable, editable suggestion should appear. Exact spelling
    // is not asserted — real OCR may misread a letter (e.g. "Sucumber"), which
    // is still useful as a prefill the user corrects.
    expect(result, isNotNull, reason: 'Tesseract produced no suggestion for '
        '$asset (see the printed value above)');
  }

  testWidgets('suggests a name from the AUBERGINE packet photo', (_) async {
    await reportRead('aubergine.jpg');
  });

  testWidgets('suggests a name from the CUCUMBER packet photo', (_) async {
    await reportRead('cucumber.jpg');
  });
}
