import 'package:cloud_firestore/cloud_firestore.dart';

enum PaymentStatus { pending, completed, failed, refunded }

class Booking {
  final String id;
  final String doctorId;
  final String profileImageUrl;
  final String doctorName;
  final String patientId;
  final String patientName;
  final String location;
  final DateTime appointmentDate;
  final String appointmentTime;
  final int consultationFee;
  final PaymentStatus paymentStatus;
  final DateTime createdAt;
  final String specialization;

  Booking({
    required this.id,
    required this.doctorId,
    required this.profileImageUrl,
    required this.doctorName,
    required this.patientId,
    required this.patientName,
    required this.location,
    required this.appointmentDate,
    required this.appointmentTime,
    required this.consultationFee,
    required this.paymentStatus,
    required this.createdAt,
    required this.specialization,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'doctorId': doctorId,
      'profileImageUrl': profileImageUrl,
      'doctorName': doctorName,
      'patientId': patientId,
      'patientName': patientName,
      'location': location,
      'appointmentDate': appointmentDate,
      'appointmentTime': appointmentTime,
      'consultationFee': consultationFee,
      'paymentStatus': paymentStatus.toString().split('.').last,
      'createdAt': createdAt,
      'specialization': specialization,
    };
  }

  factory Booking.fromMap(Map<String, dynamic> map) {
    return Booking(
      id: map['id'],
      doctorId: map['doctorId'],
      profileImageUrl: map['profileImageUrl'],
      doctorName: map['doctorName'],
      patientId: map['patientId'],
      patientName: map['patientName'],
      location: map['location'],
      appointmentDate: (map['appointmentDate'] as Timestamp).toDate(),
      appointmentTime: map['appointmentTime'],
      consultationFee: map['consultationFee'],
      paymentStatus: PaymentStatus.values.firstWhere(
        (e) => e.toString().split('.').last == map['paymentStatus'],
      ),
      createdAt: (map['createdAt'] as Timestamp).toDate(),
      specialization: map['specialization'],
    );
  }

  Booking copyWith({
    String? id,
    String? doctorId,
    String? doctorName,
    String? patientId,
    String? patientName,
    String? location,
    DateTime? appointmentDate,
    String? appointmentTime,
    int? consultationFee,
    PaymentStatus? paymentStatus,
    DateTime? createdAt,
    String? specialization,
  }) {
    return Booking(
      id: id ?? this.id,
      doctorId: doctorId ?? this.doctorId,
      profileImageUrl: profileImageUrl,
      doctorName: doctorName ?? this.doctorName,
      patientId: patientId ?? this.patientId,
      patientName: patientName ?? this.patientName,
      location: location ?? this.location,
      appointmentDate: appointmentDate ?? this.appointmentDate,
      appointmentTime: appointmentTime ?? this.appointmentTime,
      consultationFee: consultationFee!,
      paymentStatus: paymentStatus ?? this.paymentStatus,
      createdAt: createdAt ?? this.createdAt,
      specialization: specialization ?? this.specialization,
    );
  }
}
