import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/features/auth/presentation/qr_camera_bootstrap_policy.dart';

void main() {
  group('bootstrapQrCamera', () {
    test('권한 허용 후 카메라 실행 성공 시 ready 상태를 반환한다', () async {
      var stopCalled = false;

      final result = await bootstrapQrCamera(
        hasBarcodeDetector: true,
        openCameraAndPlay: () async {},
        stopCamera: () async {
          stopCalled = true;
        },
      );

      expect(result.status, QrCameraBootstrapStatus.ready);
      expect(stopCalled, isFalse);
    });

    test('권한 허용 후 play 단계 오류는 cameraError로 분류한다', () async {
      var stopCalled = false;

      final result = await bootstrapQrCamera(
        hasBarcodeDetector: true,
        openCameraAndPlay: () async {
          throw Exception('NotReadableError: Could not start video source');
        },
        stopCamera: () async {
          stopCalled = true;
        },
      );

      expect(result.status, QrCameraBootstrapStatus.cameraError);
      expect(result.errorDetail, contains('NotReadableError'));
      expect(stopCalled, isFalse);
    });

    test('권한 거부 오류는 permissionError로 분류한다', () async {
      final result = await bootstrapQrCamera(
        hasBarcodeDetector: true,
        openCameraAndPlay: () async {
          throw Exception('NotAllowedError: Permission denied');
        },
        stopCamera: () async {},
      );

      expect(result.status, QrCameraBootstrapStatus.permissionError);
      expect(result.errorDetail, contains('NotAllowedError'));
    });

    test('detector 미지원이면 카메라를 정리하고 detectorUnsupported를 반환한다', () async {
      var stopCalled = false;

      final result = await bootstrapQrCamera(
        hasBarcodeDetector: false,
        openCameraAndPlay: () async {},
        stopCamera: () async {
          stopCalled = true;
        },
      );

      expect(result.status, QrCameraBootstrapStatus.detectorUnsupported);
      expect(stopCalled, isTrue);
    });
  });
}
