import 'package:cloud_firestore/cloud_firestore.dart';

class RoomModel {
  final String id;
  final String roomName;
  final int capacity;
  final DateTime createdAt;

  RoomModel({
    required this.id,
    required this.roomName,
    required this.capacity,
    required this.createdAt,
  });

  factory RoomModel.fromFirestore(DocumentSnapshot doc) {
    final data = doc.data() as Map<String, dynamic>;
    return RoomModel(
      id: doc.id,
      roomName: data['room_name'] ?? '',
      capacity: data['capacity'] ?? 0,
      createdAt: (data['created_at'] as Timestamp).toDate(),
    );
  }

  Map<String, dynamic> toFirestore() {
    return {
      'room_name': roomName,
      'capacity': capacity,
      'created_at': Timestamp.fromDate(createdAt),
    };
  }

  factory RoomModel.fromMap(String id, Map<String, dynamic> data) {
    return RoomModel(
      id: id,
      roomName: data['room_name'] ?? '',
      capacity: data['capacity'] ?? 0,
      createdAt: data['created_at'] is Timestamp
          ? (data['created_at'] as Timestamp).toDate()
          : DateTime.parse(data['created_at']),
    );
  }

  RoomModel copyWith({
    String? id,
    String? roomName,
    int? capacity,
    DateTime? createdAt,
  }) {
    return RoomModel(
      id: id ?? this.id,
      roomName: roomName ?? this.roomName,
      capacity: capacity ?? this.capacity,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  String toString() {
    return 'RoomModel(id: $id, roomName: $roomName, capacity: $capacity, createdAt: $createdAt)';
  }
}