import 'group_member.dart';

class Group {
  final String id; 
  final String name;
  final String tripId;
  final String createdBy;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<GroupMember> members;

  Group({
    required this.id, 
    required this.name,
    required this.tripId,
    required this.createdBy,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<GroupMember>? members,
  })  : createdAt = createdAt ?? DateTime.now(),
        updatedAt = updatedAt ?? DateTime.now(),
        members = members ?? [];

  factory Group.fromMap(Map<String, dynamic> map, String id) {
    return Group(
      id: id,
      name: map['name'] ?? '',
      tripId: map['tripId'] ?? '',
      createdBy: map['createdBy'] ?? '',
      createdAt: DateTime.fromMillisecondsSinceEpoch(map['createdAt'] ?? 0),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] ?? 0),
      members: [],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'tripId': tripId,
      'createdBy': createdBy,
      'createdAt': createdAt.millisecondsSinceEpoch,
      'updatedAt': updatedAt.millisecondsSinceEpoch,
    };
  }

  Group copyWith({
    String? id,
    String? name,
    String? tripId,
    String? createdBy,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<GroupMember>? members,
  }) {
    return Group(
      id: id ?? this.id,
      name: name ?? this.name,
      tripId: tripId ?? this.tripId,
      createdBy: createdBy ?? this.createdBy,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      members: members ?? this.members,
    );
  }
}