class Player {
  const Player({
    required this.id,
    required this.name,
    this.position,
    this.avatarUrl,
    this.jerseyNumber,
    this.goalsScored = 0,
    this.assists = 0,
  });

  final String id;
  final String name;
  final String? position;
  final String? avatarUrl;
  final int? jerseyNumber;
  final int goalsScored;
  final int assists;

  Player copyWith({
    String? id,
    String? name,
    String? position,
    String? avatarUrl,
    int? jerseyNumber,
    int? goalsScored,
    int? assists,
  }) {
    return Player(
      id: id ?? this.id,
      name: name ?? this.name,
      position: position ?? this.position,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      jerseyNumber: jerseyNumber ?? this.jerseyNumber,
      goalsScored: goalsScored ?? this.goalsScored,
      assists: assists ?? this.assists,
    );
  }

  factory Player.fromMap(Map<String, dynamic> data) {
    return Player(
      id: (data['id'] as String?) ?? '',
      name: (data['name'] as String?) ?? '',
      position: data['position'] as String?,
      avatarUrl: data['avatar_url'] as String?,
      jerseyNumber: (data['jersey_number'] as num?)?.toInt(),
      goalsScored: (data['goals_scored'] as num?)?.toInt() ?? 0,
      assists: (data['assists'] as num?)?.toInt() ?? 0,
    );
  }

  Map<String, Object?> toMap() {
    return <String, Object?>{
      'id': id,
      'name': name,
      'position': position,
      'avatar_url': avatarUrl,
      'jersey_number': jerseyNumber,
      'goals_scored': goalsScored,
      'assists': assists,
    };
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Player &&
        other.id == id &&
        other.name == name &&
        other.position == position &&
        other.avatarUrl == avatarUrl &&
        other.jerseyNumber == jerseyNumber &&
        other.goalsScored == goalsScored &&
        other.assists == assists;
  }

  @override
  int get hashCode => Object.hash(
        id, name, position, avatarUrl, jerseyNumber, goalsScored, assists,
      );

  @override
  String toString() => 'Player(id: $id, name: $name)';
}
