import 'package:equatable/equatable.dart'; import 'package:professional_repository/src/entities/schedule_entity.dart'; class Schedules extends Equatable { final ScheduleEntity monday; final ScheduleEntity tuesday; final ScheduleEntity wednesday; final ScheduleEntity thursday; final ScheduleEntity friday; final ScheduleEntity saturday; final ScheduleEntity sunday; const Schedules({ required this.monday, required this.tuesday, required this.wednesday, required this.thursday, required this.friday, required this.saturday, required this.sunday, }); static const empty = Schedules( monday: ScheduleEntity.empty, tuesday: ScheduleEntity.empty, wednesday: ScheduleEntity.empty, thursday: ScheduleEntity.empty, friday: ScheduleEntity.empty, saturday: ScheduleEntity.empty, sunday: ScheduleEntity.empty, ); // Copy function Schedules copyWith({ ScheduleEntity? monday, ScheduleEntity? tuesday, ScheduleEntity? wednesday, ScheduleEntity? thursday, ScheduleEntity? friday, ScheduleEntity? saturday, ScheduleEntity? sunday, }) { return Schedules( monday: monday ?? this.monday, tuesday: tuesday ?? this.tuesday, wednesday: wednesday ?? this.wednesday, thursday: thursday ?? this.thursday, friday: friday ?? this.friday, saturday: saturday ?? this.saturday, sunday: sunday ?? this.sunday, ); } /// Parses the array the backend returns, keyed by `day_of_week` /// (0 = Monday … 6 = Sunday). Anything else yields [Schedules.empty] /// rather than throwing, so one malformed row cannot blank a screen. static Schedules fromApiArray(dynamic raw) { if (raw is! List || raw.isEmpty) return Schedules.empty; final byDay = >{}; for (final s in raw) { if (s is! Map) continue; final day = (s['day_of_week'] as num?)?.toInt(); if (day != null) byDay[day] = s.cast(); } ScheduleEntity entityFor(int day) { final s = byDay[day]; if (s == null) return ScheduleEntity.empty; return ScheduleEntity( enabled: s['enabled'] as bool? ?? false, continuousDay: s['continuous_day'] as bool? ?? false, range1Hour1: ScheduleEntity.parseTime(s['range1_hour1']?.toString()), range1Hour2: ScheduleEntity.parseTime(s['range1_hour2']?.toString()), range2Hour1: ScheduleEntity.parseTime(s['range2_hour1']?.toString()), range2Hour2: ScheduleEntity.parseTime(s['range2_hour2']?.toString()), ); } return Schedules( monday: entityFor(0), tuesday: entityFor(1), wednesday: entityFor(2), thursday: entityFor(3), friday: entityFor(4), saturday: entityFor(5), sunday: entityFor(6), ); } /// The array PATCH /professionals/me/schedules expects, ordered /// 0 = Monday … 6 = Sunday. /// /// The map form below is NOT accepted by the backend: sending it to /// /professionals/me returns 200 and silently discards the schedules, which /// is why editing opening hours appeared to work and never persisted. List> toSchedulesArray() { return [ monday.toScheduleDto(0), tuesday.toScheduleDto(1), wednesday.toScheduleDto(2), thursday.toScheduleDto(3), friday.toScheduleDto(4), saturday.toScheduleDto(5), sunday.toScheduleDto(6), ]; } @override List get props => [monday, tuesday, wednesday, thursday, friday, saturday, sunday]; }