import 'package:krow/core/application/common/date_time_extension.dart';

class ScheduleSlotModel {
  ScheduleSlotModel({
    required this.isWeekly,
    required this.startAt,
    required this.endAt,
    this.id,
  });

  factory ScheduleSlotModel.fromJson(Map<String, dynamic> data) {
    return ScheduleSlotModel(
      id: data['id'] as String?,
      isWeekly: data['type'] == 'weekly',
      startAt: DateTime.parse(data['start_at'] as String),
      endAt: DateTime.parse(data['end_at'] as String),
    );
  }

  final String? id;
  final bool isWeekly;
  final DateTime startAt;
  final DateTime endAt;

  // TODO(Sleep): For now has to use .replaceAll('.000', '') for trim DateTime otherwise will result in error from backend.
  Map<String, dynamic> toJson() {
    return {
      // if (id != null) 'id': id,
      'type': isWeekly ? 'weekly' : 'range',
      'start_at': startAt.toString().replaceAll('.000', ''),
      'end_at': endAt.toString().replaceAll('.000', ''),
      'day_of_week': startAt.getWeekdayId(),
    };
  }

  String getIdKey() =>
      isWeekly ? startAt.getWeekdayId() : startAt.getDayDateId();
}
