import 'package:flutter_test/flutter_test.dart';
import 'package:jiffy/jiffy.dart';
import 'package:marianum_mobile/state/app/modules/marianum_dates/bloc/marianum_dates_state.dart';
import 'package:marianum_mobile/view/pages/marianum_dates/data/event_formatter.dart';

MarianumDate _event({
  required DateTime start,
  required DateTime end,
  bool isAllDay = false,
}) => MarianumDate(
  uid: 't',
  title: 't',
  description: null,
  start: start,
  end: end,
  isAllDay: isAllDay,
);

void main() {
  setUpAll(() async {
    await Jiffy.setLocale('de');
  });

  group('EventFormatter.trailingLabel', () {
    test('all-day events show "Ganztägig"', () {
      final e = _event(
        start: DateTime(2026, 5, 8),
        end: DateTime(2026, 5, 9),
        isAllDay: true,
      );
      expect(EventFormatter.trailingLabel(e), 'Ganztägig');
    });

    test('zero-length same-day event shows a single time', () {
      final at = DateTime(2026, 5, 8, 9, 30);
      final e = _event(start: at, end: at);
      expect(EventFormatter.trailingLabel(e), '09:30');
    });

    test('same-day event shows time range', () {
      final e = _event(
        start: DateTime(2026, 5, 8, 9),
        end: DateTime(2026, 5, 8, 10, 30),
      );
      expect(EventFormatter.trailingLabel(e), '09:00–10:30');
    });

    test('multi-day event shows date+time on both sides', () {
      final e = _event(
        start: DateTime(2026, 5, 8, 9),
        end: DateTime(2026, 5, 9, 11),
      );
      expect(EventFormatter.trailingLabel(e), '08.05. 09:00–09.05. 11:00');
    });
  });

  group('EventFormatter.longRange', () {
    test('all-day single-day collapses inclusive end to start date', () {
      // ICS-style all-day: end is exclusive (next midnight). Display drops it.
      final e = _event(
        start: DateTime(2026, 5, 8),
        end: DateTime(2026, 5, 9),
        isAllDay: true,
      );
      expect(EventFormatter.longRange(e), '08.05.2026 · Ganztägig');
    });

    test(
      'all-day multi-day shows inclusive end (one day before exclusive end)',
      () {
        final e = _event(
          start: DateTime(2026, 5, 8),
          end: DateTime(2026, 5, 11), // exclusive → display "until 10.05."
          isAllDay: true,
        );
        expect(
          EventFormatter.longRange(e),
          '08.05.2026 – 10.05.2026 · Ganztägig',
        );
      },
    );

    test(
      'all-day event whose end equals start (degenerate) renders as single day',
      () {
        final e = _event(
          start: DateTime(2026, 5, 8),
          end: DateTime(2026, 5, 8),
          isAllDay: true,
        );
        expect(EventFormatter.longRange(e), '08.05.2026 · Ganztägig');
      },
    );

    test('zero-length same-day timed event shows single time', () {
      final at = DateTime(2026, 5, 8, 9, 30);
      final e = _event(start: at, end: at);
      expect(EventFormatter.longRange(e), '08.05.2026 · 09:30');
    });

    test('same-day timed event shows date · range', () {
      final e = _event(
        start: DateTime(2026, 5, 8, 9),
        end: DateTime(2026, 5, 8, 10, 30),
      );
      expect(EventFormatter.longRange(e), '08.05.2026 · 09:00 – 10:30');
    });

    test('multi-day timed event shows full datetimes on both sides', () {
      final e = _event(
        start: DateTime(2026, 5, 8, 9),
        end: DateTime(2026, 5, 9, 11),
      );
      expect(
        EventFormatter.longRange(e),
        '08.05.2026 09:00 – 09.05.2026 11:00',
      );
    });
  });
}
