import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumconnect/queries/timetable_get_week/timetable_get_week_response.dart';
import 'package:marianum_mobile/storage/timetable_settings.dart';
import 'package:marianum_mobile/view/pages/timetable/data/lesson_merger.dart';
import 'package:marianum_mobile/view/pages/timetable/data/timetable_appointment_factory.dart';
import 'package:marianum_mobile/view/pages/timetable/data/timetable_name_mode.dart';

McTimetableEntry _lesson({
  int id = 1,
  int hour = 8,
  int minute = 0,
  List<String> classNames = const ['7a'],
  List<String> subjects = const ['M'],
  List<String> rooms = const ['A101'],
}) => McTimetableEntry(
  id: id,
  date: DateTime(2026, 5, 4),
  startTime: DateTime(1970, 1, 1, hour, minute),
  endTime: DateTime(1970, 1, 1, hour, minute + 45),
  subjects: subjects,
  teachers: [McTimetableTeacher(shortName: 'MUE', displayName: 'Stefan Müller')],
  rooms: rooms,
  classNames: classNames,
  lessonType: 'LESSON',
  status: 'REGULAR',
  substitutionText: null,
  lessonText: null,
  infoText: null,
);

final _settings = TimetableSettings(
  connectDoubleLessons: false,
  timetableNameMode: TimetableNameMode.name,
);

String _location({
  required bool showClassInsteadOfTeacher,
  List<String> classNames = const ['7a'],
}) => TimetableAppointmentFactory(
  lessons: [_lesson(classNames: classNames)],
  customEvents: const [],
  subjects: const [],
  settings: _settings,
  now: DateTime(2026, 5, 4),
  showClassInsteadOfTeacher: showClassInsteadOfTeacher,
).build().single.location!;

void main() {
  group('teacher plan tile label', () {
    test('shows the teacher surname by default', () {
      expect(_location(showClassInsteadOfTeacher: false), 'A101\nMüller');
    });

    test('shows the class instead of the teacher on teacher plans', () {
      expect(_location(showClassInsteadOfTeacher: true), 'A101\n7a');
    });

    test('joins multiple classes', () {
      expect(
        _location(
          showClassInsteadOfTeacher: true,
          classNames: const ['7a', '7b'],
        ),
        'A101\n7a, 7b',
      );
    });

    test('falls back to the teacher when the entry has no class', () {
      expect(
        _location(showClassInsteadOfTeacher: true, classNames: const []),
        'A101\nMüller',
      );
    });
  });

  group('lesson merger class separation', () {
    test('does not merge back-to-back lessons of different classes', () {
      final merged = LessonMerger.merge([
        _lesson(id: 1, hour: 8, classNames: const ['7a']),
        _lesson(id: 2, hour: 8, minute: 45, classNames: const ['7b']),
      ]);
      expect(merged, hasLength(2));
    });

    test('still merges back-to-back lessons of the same class', () {
      final merged = LessonMerger.merge([
        _lesson(id: 1, hour: 8),
        _lesson(id: 2, hour: 8, minute: 45),
      ]);
      expect(merged, hasLength(1));
    });
  });
}
