import 'data/demo_absence.dart';
import 'data/demo_breaker.dart';
import 'data/demo_holidays.dart';
import 'data/demo_timetable.dart';
import 'data/demo_users.dart';

/// Single source of truth for the MarianumConnect demo responses. The demo
/// interceptor asks this for the body of any MC request. Read endpoints reuse
/// the typed fixtures and serialize them to the exact shape the matching query
/// parses (a bare list vs. an object); write/side-effect endpoints (telemetry,
/// client-errors, auth, push, feedback) have no consumed body and resolve to an
/// empty success.
class DemoMarianumConnect {
  const DemoMarianumConnect._();

  /// Body for [path] (relative to `/api/mobile/v1/`), using [query] for the
  /// timetable date window.
  static Object responseBody(String path, Map<String, dynamic> query) {
    switch (path) {
      case 'timetable/me':
        return DemoTimetable.week(
          _date(query['from']),
          _date(query['until']),
        ).toJson();
      case 'timetable/schoolyear':
        return DemoTimetable.schoolyear().toJson();
      case 'timetable/rooms':
        return DemoTimetable.rooms().result.map((e) => e.toJson()).toList();
      case 'timetable/subjects':
        return DemoTimetable.subjects().result.map((e) => e.toJson()).toList();
      case 'timetable/timegrid':
        return DemoTimetable.timegrid().result.map((e) => e.toJson()).toList();
      case 'timetable/holidays':
        return DemoTimetable.holidays().result.map((e) => e.toJson()).toList();
      case 'holidays':
        return DemoHolidays.upcoming().map((e) => e.toJson()).toList();
      case 'users/search':
        return DemoUsers.search(query['q']?.toString() ?? '')
            .map((e) => e.toJson())
            .toList();
      case 'breaker':
        return DemoBreaker.none().toJson();
      case 'absence/classes':
        return DemoAbsence.classes();
      case 'absence/prefill':
        return DemoAbsence.prefill().toJson();
      case 'timetable/elements/teachers':
      case 'timetable/elements/students':
      case 'timetable/elements/classes':
        return const <dynamic>[];
    }
    // A foreign element's week (timetable/<type>/<id>) shows the same demo week.
    if (path.startsWith('timetable/')) {
      return DemoTimetable.week(
        _date(query['from']),
        _date(query['until']),
      ).toJson();
    }
    return const <String, dynamic>{};
  }

  static DateTime _date(Object? raw) =>
      DateTime.tryParse(raw?.toString() ?? '') ?? DateTime.now();
}
