class Member {
  final String id;
  final String userId;
  final String clubId;
  final String name;
  final String email;
  final String? role;
  final String? profileImage;
  final String? phone;
  final String? address;
  final DateTime? joinDate;
  final bool isActive;
  final String? gender;
  final String? memberType;
  final int? handicap;
  final String? status;
  final double? average;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final DateTime? birthDate; // 생년월일 추가

  Member({
    required this.id,
    required this.userId,
    required this.clubId,
    required this.name,
    required this.email,
    this.role,
    this.profileImage,
    this.phone,
    this.address,
    this.joinDate,
    required this.isActive,
    this.gender,
    this.memberType,
    this.handicap,
    this.status,
    this.average,
    this.createdAt,
    this.updatedAt,
    this.birthDate,
  });

  // JSON 데이터로부터 Member 객체 생성
  factory Member.fromJson(Map<String, dynamic> json) {
    return Member(
      id: json['id']?.toString() ?? '',
      userId: json['userId']?.toString() ?? '',
      clubId: json['clubId']?.toString() ?? '',
      name: json['name'] ?? '',
      email: json['email'] ?? '',
      role: json['role']?.toString(),
      profileImage: json['profileImage']?.toString(),
      phone: json['phone']?.toString(),
      address: json['address']?.toString(),
      joinDate: json['joinDate'] != null ? DateTime.parse(json['joinDate'].toString()) : null,
      isActive: json['isActive'] ?? true,
      gender: json['gender']?.toString(),
      memberType: json['memberType']?.toString(),
      handicap: json['handicap'] != null ? int.tryParse(json['handicap'].toString()) : null,
      status: json['status']?.toString(),
      average: () {
        final val = json['average'];
        if (val == null) return null;
        if (val is num) return val.toDouble();
        return double.tryParse(val.toString());
      }(),
      createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt'].toString()) : null,
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt'].toString()) : null,
      birthDate: json['birthDate'] != null ? DateTime.parse(json['birthDate'].toString()) : null,
    );
  }

  // Member 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'clubId': clubId,
      'name': name,
      'email': email,
      'role': role,
      'profileImage': profileImage,
      'phone': phone,
      'address': address,
      'joinDate': joinDate?.toIso8601String(),
      'isActive': isActive,
      'gender': gender,
      'memberType': memberType,
      'handicap': handicap,
      'status': status,
      'average': average,
      'createdAt': createdAt?.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
      'birthDate': birthDate?.toIso8601String(),
    };
  }
  
  // 객체 복사 및 특정 필드 업데이트를 위한 copyWith 메서드
  Member copyWith({
    String? id,
    String? userId,
    String? clubId,
    String? name,
    String? email,
    String? role,
    String? profileImage,
    String? phone,
    String? address,
    DateTime? joinDate,
    bool? isActive,
    String? gender,
    String? memberType,
    int? handicap,
    String? status,
    double? average,
    DateTime? createdAt,
    DateTime? updatedAt,
    DateTime? birthDate,
  }) {
    return Member(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      clubId: clubId ?? this.clubId,
      name: name ?? this.name,
      email: email ?? this.email,
      role: role ?? this.role,
      profileImage: profileImage ?? this.profileImage,
      phone: phone ?? this.phone,
      address: address ?? this.address,
      joinDate: joinDate ?? this.joinDate,
      isActive: isActive ?? this.isActive,
      gender: gender ?? this.gender,
      memberType: memberType ?? this.memberType,
      handicap: handicap ?? this.handicap,
      status: status ?? this.status,
      average: average ?? this.average,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      birthDate: birthDate ?? this.birthDate,
    );
  }
}
