import 'package:cloud_firestore/cloud_firestore.dart';

class ScheduleModel {
  final String id;
  final String patientId;
  final String medicineDetail;
  final String fluidDetail;
  final DateTime scheduleDate;
  final String timeOfDay;
  final DateTime createdAt;

  ScheduleModel({
    required this.id,
    required this.patientId,
    required this.medicineDetail,
    required this.fluidDetail,
    required this.scheduleDate,
    required this.timeOfDay,
    required this.createdAt,
  });

  factory ScheduleModel.fromFirestore(DocumentSnapshot doc) {
    final data = doc.data() as Map<String, dynamic>;
    return ScheduleModel(
      id: doc.id,
      patientId: data['patient_id'] ?? '',
      medicineDetail: data['medicine_detail'] ?? '',
      fluidDetail: data['fluid_detail'] ?? '',
      scheduleDate: data['schedule_date'] != null
          ? (data['schedule_date'] as Timestamp).toDate()
          : DateTime.now(),
      timeOfDay: data['time_of_day'] ?? 'Pagi',
      createdAt: data['created_at'] != null
          ? (data['created_at'] as Timestamp).toDate()
          : DateTime.now(),
    );
  }

  Map<String, dynamic> toFirestore() {
    return {
      'patient_id': patientId,
      'medicine_detail': medicineDetail,
      'fluid_detail': fluidDetail,
      'schedule_date': Timestamp.fromDate(scheduleDate),
      'time_of_day': timeOfDay,
      'created_at': Timestamp.fromDate(createdAt),
    };
  }
}