import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumconnect/models/mc_holiday.dart';
import 'package:marianum_mobile/api/mhsl/custom_timetable_event/custom_timetable_event.dart';
import 'package:marianum_mobile/storage/timetable_settings.dart';
import 'package:marianum_mobile/view/pages/timetable/data/arbitrary_appointment.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';
import 'package:syncfusion_flutter_calendar/calendar.dart';

CustomTimetableEvent _recurringEvent({required bool skipHolidays}) =>
    CustomTimetableEvent(
      id: 'e1',
      title: 'Sprechstunde',
      description: '',
      startDate: DateTime(2026, 5, 4, 8, 0),
      endDate: DateTime(2026, 5, 4, 9, 0),
      color: 'orange',
      rrule: 'RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
      skipHolidays: skipHolidays,
      createdAt: DateTime(2026, 5, 4),
      updatedAt: DateTime(2026, 5, 4),
    );

final _holidays = [
  McHoliday(
    shortName: 'Pfingsten',
    longName: 'Pfingstferien',
    startDate: DateTime(2026, 5, 6),
    endDate: DateTime(2026, 5, 8),
  ),
];

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

Appointment _customAppointment(List<Appointment> appointments) =>
    appointments.firstWhere((a) => a.id is CustomAppointment);

void main() {
  group('holiday exclusion', () {
    test('adds every holiday day at the event time as a recurrence exception '
        'when skipHolidays is on', () {
      final appointments = TimetableAppointmentFactory(
        lessons: const [],
        customEvents: [_recurringEvent(skipHolidays: true)],
        subjects: const [],
        holidays: _holidays,
        settings: _settings,
        now: DateTime(2026, 5, 4),
      ).build();

      final exceptions = _customAppointment(appointments).recurrenceExceptionDates!;
      expect(exceptions, contains(DateTime(2026, 5, 6, 8, 0)));
      expect(exceptions, contains(DateTime(2026, 5, 7, 8, 0)));
      expect(exceptions, contains(DateTime(2026, 5, 8, 8, 0)));
      expect(exceptions, hasLength(3));
    });

    test('leaves recurrence exceptions empty when skipHolidays is off', () {
      final appointments = TimetableAppointmentFactory(
        lessons: const [],
        customEvents: [_recurringEvent(skipHolidays: false)],
        subjects: const [],
        holidays: _holidays,
        settings: _settings,
        now: DateTime(2026, 5, 4),
      ).build();

      expect(
        _customAppointment(appointments).recurrenceExceptionDates,
        isNull,
      );
    });
  });
}
