import 'dart:convert';
import 'group_member.dart';

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

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

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

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

  String toJson() => json.encode(toMap());

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