class Participant {
  final String id;
  final String eventId;
  final String memberId;
  final String? name;
  final String? email;
  final String? phoneNumber;
  final String? status; // 'registered', 'confirmed', 'cancelled', 'attended'
  final DateTime? registeredAt;
  final DateTime? confirmedAt;
  final DateTime? cancelledAt;
  final DateTime? attendedAt;
  final bool isPaid;
  final double? paidAmount;
  final String? notes;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  String? memberType; // 회원 유형 필드 추가: '정회원', '준회원', '게스트' 등

  Participant({
    required this.id,
    required this.eventId,
    required this.memberId,
    this.name,
    this.email,
    this.phoneNumber,
    this.status,
    this.registeredAt,
    this.confirmedAt,
    this.cancelledAt,
    this.attendedAt,
    this.isPaid = false,
    this.paidAmount,
    this.notes,
    this.createdAt,
    this.updatedAt,
  });

  // JSON 데이터로부터 Participant 객체 생성
  factory Participant.fromJson(Map<String, dynamic> json) {
    return Participant(
      id: json['id']?.toString() ?? '',
      eventId: json['eventId']?.toString() ?? '',
      memberId: json['memberId']?.toString() ?? '',
      // 서버 응답에 Member가 중첩되는 경우 보정
      name: (json['name'] ?? json['Member']?['name'])?.toString(),
      email: (json['email'] ?? json['Member']?['email'])?.toString(),
      phoneNumber: (json['phoneNumber'] ?? json['phone'] ?? json['Member']?['phone'])?.toString(),
      status: json['status']?.toString(),
      registeredAt: json['registeredAt'] != null ? DateTime.parse(json['registeredAt'].toString()) : null,
      confirmedAt: json['confirmedAt'] != null ? DateTime.parse(json['confirmedAt'].toString()) : null,
      cancelledAt: json['cancelledAt'] != null ? DateTime.parse(json['cancelledAt'].toString()) : null,
      attendedAt: json['attendedAt'] != null ? DateTime.parse(json['attendedAt'].toString()) : null,
      // 서버는 paymentStatus('paid'|'unpaid')를 줄 수 있으므로 이를 isPaid로 변환
      isPaid: json['isPaid'] ?? (json['paymentStatus']?.toString().toLowerCase() == 'paid'),
      paidAmount: json['paidAmount'] != null ? double.tryParse(json['paidAmount'].toString()) : null,
      notes: json['notes']?.toString(),
      createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt'].toString()) : null,
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt'].toString()) : null,
    );
  }

  // Participant 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'eventId': eventId,
      'memberId': memberId,
      'name': name,
      'email': email,
      'phoneNumber': phoneNumber,
      'status': status,
      'registeredAt': registeredAt?.toIso8601String(),
      'confirmedAt': confirmedAt?.toIso8601String(),
      'cancelledAt': cancelledAt?.toIso8601String(),
      'attendedAt': attendedAt?.toIso8601String(),
      'isPaid': isPaid,
      'paidAmount': paidAmount,
      'notes': notes,
      'createdAt': createdAt?.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
    };
  }
}
