import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/date_time_utils.dart';

void main() {
  group('DateTimeUtils', () {
    group('toDateTime', () {
      test('returns null when element is null', () {
        expect(DateTimeUtils.toDateTime(null), isNull);
      });

      test('returns null when element cannot be parsed to int', () {
        expect(DateTimeUtils.toDateTime('invalid'), isNull);
        expect(DateTimeUtils.toDateTime('abc123'), isNull);
        expect(DateTimeUtils.toDateTime('12.34'), isNull);
      });

      test('converts valid milliseconds string to DateTime', () {
        const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
        final result = DateTimeUtils.toDateTime(milliseconds.toString());

        expect(result, isNotNull);
        expect(result!.millisecondsSinceEpoch, equals(milliseconds));
      });

      test('converts valid milliseconds int to DateTime', () {
        const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
        final result = DateTimeUtils.toDateTime(milliseconds);

        expect(result, isNotNull);
        expect(result!.millisecondsSinceEpoch, equals(milliseconds));
      });

      test('handles zero milliseconds', () {
        final result = DateTimeUtils.toDateTime(0);

        expect(result, isNotNull);
        expect(result!.millisecondsSinceEpoch, equals(0));
      });

      test('handles negative milliseconds', () {
        const milliseconds = -1000;
        final result = DateTimeUtils.toDateTime(milliseconds);

        expect(result, isNotNull);
        expect(result!.millisecondsSinceEpoch, equals(milliseconds));
      });
    });

    group('fromDateTime', () {
      test('returns null when DateTime is null', () {
        expect(DateTimeUtils.fromDateTime(null), isNull);
      });

      test('converts DateTime to milliseconds since epoch', () {
        const milliseconds = 1640995200000; // 2022-01-01 00:00:00 UTC
        final dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);

        final result = DateTimeUtils.fromDateTime(dateTime);

        expect(result, equals(milliseconds));
      });

      test('handles epoch time (zero)', () {
        final dateTime = DateTime.fromMillisecondsSinceEpoch(0);

        final result = DateTimeUtils.fromDateTime(dateTime);

        expect(result, equals(0));
      });

      test('handles dates before epoch (negative milliseconds)', () {
        const milliseconds = -1000;
        final dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);

        final result = DateTimeUtils.fromDateTime(dateTime);

        expect(result, equals(milliseconds));
      });
    });

    group('round-trip conversion', () {
      test('toDateTime and fromDateTime are inverse operations', () {
        const originalMilliseconds = 1640995200000;

        final dateTime = DateTimeUtils.toDateTime(originalMilliseconds);
        final convertedBack = DateTimeUtils.fromDateTime(dateTime);

        expect(convertedBack, equals(originalMilliseconds));
      });

      test('fromDateTime and toDateTime are inverse operations', () {
        final originalDateTime = DateTime.now();

        final milliseconds = DateTimeUtils.fromDateTime(originalDateTime);
        final convertedBack = DateTimeUtils.toDateTime(milliseconds);

        expect(convertedBack?.millisecondsSinceEpoch, equals(originalDateTime.millisecondsSinceEpoch));
      });
    });
  });
}