class Event {
  final String id;
  final String clubId;
  final String title;
  final String? description;
  final DateTime startDate;
  final DateTime? endDate;
  final String? location;
  final String? type;
  final String? status;
  final int? maxParticipants;
  final int? currentParticipants;
  final int? gameCount;
  final double? participantFee;
  final DateTime? registrationDeadline;
  final String? publicHash;
  final String? accessPassword;
  final bool isActive;
  final String? createdBy;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  Event({
    required this.id,
    required this.clubId,
    required this.title,
    this.description,
    required this.startDate,
    this.endDate,
    this.location,
    this.type,
    this.status,
    this.maxParticipants,
    this.currentParticipants,
    this.gameCount,
    this.participantFee,
    this.registrationDeadline,
    this.publicHash,
    this.accessPassword,
    required this.isActive,
    this.createdBy,
    this.createdAt,
    this.updatedAt,
  });

  // JSON 데이터로부터 Event 객체 생성
  factory Event.fromJson(Map<String, dynamic> json) {
    return Event(
      id: json['id']?.toString() ?? '',
      clubId: json['clubId']?.toString() ?? '',
      title: json['title'] ?? '',
      description: json['description']?.toString(),
      startDate: json['startDate'] != null ? DateTime.parse(json['startDate'].toString()) : DateTime.now(),
      endDate: json['endDate'] != null ? DateTime.parse(json['endDate'].toString()) : null,
      location: json['location']?.toString(),
      // 서버는 eventType 키를 사용할 수 있으므로 보강
      type: (json['type'] ?? json['eventType'])?.toString(),
      status: json['status']?.toString(),
      maxParticipants: json['maxParticipants'],
      currentParticipants: json['currentParticipants'],
      gameCount: json['gameCount'],
      // 참가비: 서버가 "0"/"0.00" 등으로 기본화할 수 있어 0은 null로 간주
      participantFee: Event._parseParticipantFeeOrNull(json['participantFee']),
      registrationDeadline: json['registrationDeadline'] != null ? DateTime.parse(json['registrationDeadline'].toString()) : null,
      publicHash: json['publicHash']?.toString(),
      accessPassword: json['accessPassword']?.toString(),
      isActive: json['isActive'] ?? true,
      createdBy: json['createdBy']?.toString(),
      createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt'].toString()) : null,
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt'].toString()) : null,
    );
  }

  // 참가비 파싱 헬퍼: null/빈문자열/0/"0.00" 모두 null로 간주
  static double? _parseParticipantFeeOrNull(dynamic value) {
    if (value == null) return null;
    final s = value.toString().trim();
    if (s.isEmpty) return null;
    final d = double.tryParse(s);
    if (d == null) return null;
    // 0도 유효한 값으로 취급하여 표시되도록 그대로 반환
    return d;
  }

 

  // Event 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'clubId': clubId,
      'title': title,
      'description': description,
      'startDate': startDate.toIso8601String(),
      'endDate': endDate?.toIso8601String(),
      'location': location,
      'type': type,
      'status': status,
      'maxParticipants': maxParticipants,
      'currentParticipants': currentParticipants,
      'gameCount': gameCount,
      'participantFee': participantFee,
      'registrationDeadline': registrationDeadline?.toIso8601String(),
      'publicHash': publicHash,
      'accessPassword': accessPassword,
      'isActive': isActive,
      'createdBy': createdBy,
      'createdAt': createdAt?.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
    };
  }
}
