import '../../../../../api/marianumconnect/queries/timetable_get_element_week/timetable_element_type.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_element_week/timetable_get_element_week.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_holidays/timetable_get_holidays_response.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_rooms/timetable_get_rooms_response.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_schoolyear/timetable_get_schoolyear_response.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_subjects/timetable_get_subjects_response.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_timegrid/timetable_get_timegrid_response.dart';
import '../../../../../api/marianumconnect/queries/timetable_get_week/timetable_get_week_response.dart';
import '../../timetable/data_provider/timetable_data_provider.dart';

/// Data access for a foreign element's timetable. The week comes from the
/// element-specific endpoint; all reference data (rooms/subjects/holidays/
/// school year/timegrid) is school-wide, so it delegates to the existing
/// [TimetableDataProvider] (which caches it). Custom events are intentionally
/// absent — they are user-private.
class ForeignTimetableDataProvider {
  final TimetableDataProvider _base;

  ForeignTimetableDataProvider([TimetableDataProvider? base])
    : _base = base ?? TimetableDataProvider();

  Future<TimetableGetWeekResponse> getElementWeek(
    TimetableElementType type,
    int id,
    DateTime startDate,
    DateTime endDate, {
    void Function(Object)? onError,
  }) async {
    try {
      return await TimetableGetElementWeek().run(
        type: type,
        id: id,
        from: startDate,
        until: endDate,
      );
    } catch (e) {
      onError?.call(e);
      rethrow;
    }
  }

  Future<TimetableGetRoomsResponse> getRooms({
    void Function(Object)? onError,
    bool renew = false,
  }) => _base.getRooms(onError: onError, renew: renew);

  Future<TimetableGetSubjectsResponse> getSubjects({
    void Function(Object)? onError,
    bool renew = false,
  }) => _base.getSubjects(onError: onError, renew: renew);

  Future<TimetableGetHolidaysResponse> getSchoolHolidays({
    void Function(Object)? onError,
    bool renew = false,
  }) => _base.getSchoolHolidays(onError: onError, renew: renew);

  Future<TimetableGetSchoolyearResponse> getCurrentSchoolyear({
    void Function(Object)? onError,
    bool renew = false,
  }) => _base.getCurrentSchoolyear(onError: onError, renew: renew);

  Future<TimetableGetTimegridResponse> getTimegrid({bool renew = false}) =>
      _base.getTimegrid(renew: renew);
}
