/// Tests unitaires pour ErrorHandler
library error_handler_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:dio/dio.dart';
import 'package:unionflow_mobile_apps/core/error/error_handler.dart';

void main() {
  group('ErrorHandler', () {
    group('getErrorMessage', () {
      test('retourne message pour String', () {
        const error = 'Erreur personnalisée';
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, equals('Erreur personnalisée'));
      });

      test('retourne message pour Exception', () {
        final error = Exception('Erreur test');
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, equals('Erreur test'));
      });

      test('retourne message par défaut pour erreur inconnue', () {
        final error = Object();
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, equals('Une erreur inattendue s\'est produite.'));
      });

      test('gère DioException connectionTimeout', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.connectionTimeout,
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Délai de connexion dépassé'));
      });

      test('gère DioException sendTimeout', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.sendTimeout,
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Délai d\'envoi dépassé'));
      });

      test('gère DioException receiveTimeout', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.receiveTimeout,
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Délai de réception dépassé'));
      });

      test('gère DioException cancel', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.cancel,
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, equals('Requête annulée.'));
      });

      test('gère DioException connectionError', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.connectionError,
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Erreur de connexion'));
      });

      test('gère HTTP 400 Bad Request', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 400,
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Requête invalide'));
      });

      test('gère HTTP 401 Unauthorized', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 401,
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Non authentifié'));
      });

      test('gère HTTP 403 Forbidden', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 403,
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Accès refusé'));
      });

      test('gère HTTP 404 Not Found', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 404,
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Ressource non trouvée'));
      });

      test('gère HTTP 500 Internal Server Error', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 500,
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, contains('Erreur serveur'));
      });

      test('extrait message personnalisé du body', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 400,
            data: {'message': 'Message personnalisé du serveur'},
          ),
        );
        final message = ErrorHandler.getErrorMessage(error);
        expect(message, equals('Message personnalisé du serveur'));
      });
    });

    group('isNetworkError', () {
      test('retourne true pour connectionTimeout', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.connectionTimeout,
        );
        expect(ErrorHandler.isNetworkError(error), isTrue);
      });

      test('retourne true pour connectionError', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.connectionError,
        );
        expect(ErrorHandler.isNetworkError(error), isTrue);
      });

      test('retourne false pour badResponse', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 400,
          ),
        );
        expect(ErrorHandler.isNetworkError(error), isFalse);
      });

      test('retourne false pour non-DioException', () {
        final error = Exception('Test');
        expect(ErrorHandler.isNetworkError(error), isFalse);
      });
    });

    group('isAuthError', () {
      test('retourne true pour HTTP 401', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 401,
          ),
        );
        expect(ErrorHandler.isAuthError(error), isTrue);
      });

      test('retourne false pour HTTP 403', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 403,
          ),
        );
        expect(ErrorHandler.isAuthError(error), isFalse);
      });
    });

    group('isPermissionError', () {
      test('retourne true pour HTTP 403', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 403,
          ),
        );
        expect(ErrorHandler.isPermissionError(error), isTrue);
      });

      test('retourne false pour HTTP 401', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 401,
          ),
        );
        expect(ErrorHandler.isPermissionError(error), isFalse);
      });
    });

    group('isValidationError', () {
      test('retourne true pour HTTP 400', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 400,
          ),
        );
        expect(ErrorHandler.isValidationError(error), isTrue);
      });

      test('retourne true pour HTTP 422', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 422,
          ),
        );
        expect(ErrorHandler.isValidationError(error), isTrue);
      });

      test('retourne false pour HTTP 500', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 500,
          ),
        );
        expect(ErrorHandler.isValidationError(error), isFalse);
      });
    });

    group('isServerError', () {
      test('retourne true pour HTTP 500', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 500,
          ),
        );
        expect(ErrorHandler.isServerError(error), isTrue);
      });

      test('retourne true pour HTTP 503', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 503,
          ),
        );
        expect(ErrorHandler.isServerError(error), isTrue);
      });

      test('retourne false pour HTTP 400', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 400,
          ),
        );
        expect(ErrorHandler.isServerError(error), isFalse);
      });
    });

    group('ErrorHandlerExtension', () {
      test('toErrorMessage fonctionne', () {
        const error = 'Test error';
        expect(error.toErrorMessage(), equals('Test error'));
      });

      test('isNetworkError fonctionne', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.connectionTimeout,
        );
        expect(error.isNetworkError, isTrue);
      });

      test('isAuthError fonctionne', () {
        final error = DioException(
          requestOptions: RequestOptions(path: '/test'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/test'),
            statusCode: 401,
          ),
        );
        expect(error.isAuthError, isTrue);
      });
    });
  });
}

