enum JoinRequestStatus { pending, approved, rejected }

JoinRequestStatus joinRequestStatusFromString(String? raw) {
  switch (raw) {
    case 'approved':
      return JoinRequestStatus.approved;
    case 'rejected':
      return JoinRequestStatus.rejected;
    default:
      return JoinRequestStatus.pending;
  }
}

class JoinRequest {
  const JoinRequest({
    required this.id,
    required this.teamId,
    required this.teamName,
    required this.playerId,
    required this.playerName,
    required this.playerEmail,
    required this.status,
    required this.requestedAt,
  });

  final String id;
  final String teamId;
  final String teamName;
  final String playerId;
  final String playerName;
  final String playerEmail;
  final JoinRequestStatus status;
  final DateTime requestedAt;

  JoinRequest copyWith({
    String? id,
    String? teamId,
    String? teamName,
    String? playerId,
    String? playerName,
    String? playerEmail,
    JoinRequestStatus? status,
    DateTime? requestedAt,
  }) {
    return JoinRequest(
      id: id ?? this.id,
      teamId: teamId ?? this.teamId,
      teamName: teamName ?? this.teamName,
      playerId: playerId ?? this.playerId,
      playerName: playerName ?? this.playerName,
      playerEmail: playerEmail ?? this.playerEmail,
      status: status ?? this.status,
      requestedAt: requestedAt ?? this.requestedAt,
    );
  }

  factory JoinRequest.fromJson(Map<String, dynamic> data) {
    return JoinRequest(
      id: (data['id'] as String?) ?? '',
      teamId: (data['team_id'] as String?) ?? '',
      teamName: (data['team_name'] as String?) ?? '',
      playerId: (data['player_id'] as String?) ?? '',
      playerName: (data['player_name'] as String?) ?? '',
      playerEmail: (data['player_email'] as String?) ?? '',
      status: joinRequestStatusFromString(data['status'] as String?),
      requestedAt: _parseDate(data['requested_at']) ?? DateTime.now(),
    );
  }

  Map<String, Object?> toJson() {
    return <String, Object?>{
      'team_id': teamId,
      'team_name': teamName,
      'player_id': playerId,
      'player_name': playerName,
      'player_email': playerEmail,
      'status': status.name,
      'requested_at': requestedAt.toIso8601String(),
    };
  }

  static DateTime? _parseDate(Object? v) {
    if (v is String && v.isNotEmpty) return DateTime.tryParse(v);
    return null;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is JoinRequest &&
        other.id == id &&
        other.teamId == teamId &&
        other.teamName == teamName &&
        other.playerId == playerId &&
        other.playerName == playerName &&
        other.playerEmail == playerEmail &&
        other.status == status &&
        other.requestedAt == requestedAt;
  }

  @override
  int get hashCode => Object.hash(
        id, teamId, teamName, playerId,
        playerName, playerEmail, status, requestedAt,
      );

  @override
  String toString() =>
      'JoinRequest(id: $id, team: $teamName, player: $playerName, '
      'status: ${status.name})';
}
