import '../../../../extensions/date_time.dart';
import '../../../../state/app/modules/marianum_dates/bloc/marianum_dates_state.dart';

/// Pure formatting helpers for `MarianumDate` events. Held outside the view
/// so the view can stay focused on layout and these helpers remain
/// unit-testable.
class EventFormatter {
  /// Compact trailing label shown in the list row: "HH:mm–HH:mm" for same-day,
  /// "dd.MM. HH:mm–dd.MM. HH:mm" otherwise, or "Ganztägig" for all-day events.
  static String trailingLabel(MarianumDate event) {
    if (event.isAllDay) return 'Ganztägig';
    if (event.start.isSameDay(event.end)) {
      if (event.start == event.end) return event.start.formatHm();
      return '${event.start.formatHm()}–${event.end.formatHm()}';
    }
    return '${event.start.formatDateShortHm()}–${event.end.formatDateShortHm()}';
  }

  /// Verbose date+time line shown in the details sheet. Drops the trailing
  /// time when the event is all-day, and de-duplicates same-day endpoints.
  static String longRange(MarianumDate event) {
    if (event.isAllDay) {
      final inclusiveEnd = event.end.isAfter(event.start)
          ? event.end.subtract(const Duration(days: 1))
          : event.end;
      return event.start.isSameDay(inclusiveEnd)
          ? '${event.start.formatDate()} · Ganztägig'
          : '${event.start.formatDate()} – ${inclusiveEnd.formatDate()} · Ganztägig';
    }
    if (event.start.isSameDay(event.end)) {
      if (event.start == event.end) {
        return '${event.start.formatDate()} · ${event.start.formatHm()}';
      }
      return '${event.start.formatDate()} · ${event.start.formatHm()} – ${event.end.formatHm()}';
    }
    return '${event.start.formatDateTime()} – ${event.end.formatDateTime()}';
  }
}
