import '../../../../api/marianumconnect/queries/timetable_get_week/timetable_get_week_response.dart';

enum LessonStatus {
  cancelled,
  event,
  irregular,
  teacherChanged,
  duty,
  past,
  ongoing,
  regular,
}

class LessonStatusClassifier {
  /// Mirrors the legacy Webuntis classifier: cancelled trumps everything,
  /// then event (subject-less lessons such as Wandertag), then irregular
  /// (status from the backend or a slot without an assigned teacher), then
  /// teacherChanged when the backend reports a substitution swap, then
  /// duty (Aufsicht/Sprechstunde/…) so they stand out from regular
  /// classroom lessons, then the time-based past/ongoing/regular states.
  static LessonStatus classify(
    McTimetableEntry entry,
    DateTime startTime,
    DateTime endTime,
    DateTime now, {
    bool isEvent = false,
    bool isDuty = false,
  }) {
    if (entry.status == 'CANCELLED') return LessonStatus.cancelled;
    if (isEvent) return LessonStatus.event;
    if (entry.status == 'IRREGULAR' || entry.teachers.isEmpty) {
      return LessonStatus.irregular;
    }
    if (entry.teachers.any((t) => (t.originalShortName ?? '').isNotEmpty)) {
      return LessonStatus.teacherChanged;
    }
    if (isDuty) return LessonStatus.duty;
    if (endTime.isBefore(now)) return LessonStatus.past;
    if (startTime.isBefore(now) && endTime.isAfter(now)) {
      return LessonStatus.ongoing;
    }
    return LessonStatus.regular;
  }
}
