import 'package:afterwork/core/utils/calculate_time_ago.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('calculateTimeAgo', () {
    test('should return "À l\'instant" for dates less than a minute ago', () {
      // Arrange
      final now = DateTime.now();
      final recentDate = now.subtract(const Duration(seconds: 30));

      // Act
      final result = calculateTimeAgo(recentDate);

      // Assert
      expect(result, 'À l\'instant');
    });

    test('should return "il y a 1 minute" for date 1 minute ago', () {
      // Arrange
      final now = DateTime.now();
      final oneMinuteAgo = now.subtract(const Duration(minutes: 1));

      // Act
      final result = calculateTimeAgo(oneMinuteAgo);

      // Assert
      expect(result, 'il y a 1 minute');
    });

    test('should return "il y a X minutes" for dates multiple minutes ago', () {
      // Arrange
      final now = DateTime.now();
      final fiveMinutesAgo = now.subtract(const Duration(minutes: 5));
      final thirtyMinutesAgo = now.subtract(const Duration(minutes: 30));

      // Act
      final result5 = calculateTimeAgo(fiveMinutesAgo);
      final result30 = calculateTimeAgo(thirtyMinutesAgo);

      // Assert
      expect(result5, 'il y a 5 minutes');
      expect(result30, 'il y a 30 minutes');
    });

    test('should return "il y a 1 heure" for date 1 hour ago', () {
      // Arrange
      final now = DateTime.now();
      final oneHourAgo = now.subtract(const Duration(hours: 1));

      // Act
      final result = calculateTimeAgo(oneHourAgo);

      // Assert
      expect(result, 'il y a 1 heure');
    });

    test('should return "il y a X heures" for dates multiple hours ago', () {
      // Arrange
      final now = DateTime.now();
      final twoHoursAgo = now.subtract(const Duration(hours: 2));
      final twelveHoursAgo = now.subtract(const Duration(hours: 12));

      // Act
      final result2 = calculateTimeAgo(twoHoursAgo);
      final result12 = calculateTimeAgo(twelveHoursAgo);

      // Assert
      expect(result2, 'il y a 2 heures');
      expect(result12, 'il y a 12 heures');
    });

    test('should return "il y a 1 jour" for date 1 day ago', () {
      // Arrange
      final now = DateTime.now();
      final oneDayAgo = now.subtract(const Duration(days: 1));

      // Act
      final result = calculateTimeAgo(oneDayAgo);

      // Assert
      expect(result, 'il y a 1 jour');
    });

    test('should return "il y a X jours" for dates multiple days ago', () {
      // Arrange
      final now = DateTime.now();
      final twoDaysAgo = now.subtract(const Duration(days: 2));
      final sevenDaysAgo = now.subtract(const Duration(days: 7));
      final thirtyDaysAgo = now.subtract(const Duration(days: 30));

      // Act
      final result2 = calculateTimeAgo(twoDaysAgo);
      final result7 = calculateTimeAgo(sevenDaysAgo);
      final result30 = calculateTimeAgo(thirtyDaysAgo);

      // Assert
      expect(result2, 'il y a 2 jours');
      expect(result7, 'il y a 7 jours');
      // 30 jours = 4 semaines (la fonction priorise les semaines pour > 7 jours)
      expect(result30, 'il y a 4 semaines');
    });

    test('should prioritize days over hours', () {
      // Arrange
      final now = DateTime.now();
      final oneDayAndOneHourAgo = now.subtract(
        const Duration(days: 1, hours: 1),
      );

      // Act
      final result = calculateTimeAgo(oneDayAndOneHourAgo);

      // Assert
      expect(result, contains('jour'));
      expect(result, isNot(contains('heure')));
    });

    test('should prioritize hours over minutes', () {
      // Arrange
      final now = DateTime.now();
      final oneHourAndOneMinuteAgo = now.subtract(
        const Duration(hours: 1, minutes: 1),
      );

      // Act
      final result = calculateTimeAgo(oneHourAndOneMinuteAgo);

      // Assert
      expect(result, contains('heure'));
      expect(result, isNot(contains('minute')));
    });

    test('should handle future dates correctly', () {
      // Arrange
      final now = DateTime.now();
      final futureDate = now.add(const Duration(minutes: 5));

      // Act
      final result = calculateTimeAgo(futureDate);

      // Assert
      // Should return "À l'instant" for negative differences
      expect(result, isA<String>());
    });
  });
}

