import 'package:dio/dio.dart';
import '../../../../core/network/api_endpoints.dart';

class LeaderboardEntryModel {
  final String username;
  final int totalScore;
  final int quizzesTaken;
  final String? avatar;
  final int avgScore;

  const LeaderboardEntryModel({
    required this.username,
    required this.totalScore,
    required this.quizzesTaken,
    this.avatar,
    required this.avgScore,
  });

  factory LeaderboardEntryModel.fromJson(Map<String, dynamic> json) {
    return LeaderboardEntryModel(
      username: json['username'] as String,
      totalScore: (json['total_score'] as num).toInt(),
      quizzesTaken: (json['quizzes_taken'] as num).toInt(),
      avatar: json['avatar'] as String?,
      avgScore: (json['avg_score'] as num).toInt(),
    );
  }
}

class LeaderboardRemoteDatasource {
  final Dio _dio;

  LeaderboardRemoteDatasource(this._dio);

  Future<List<LeaderboardEntryModel>> getLeaderboard() async {
    final response = await _dio.get(ApiEndpoints.leaderboard);
    final data = response.data as Map<String, dynamic>;
    return (data['leaderboard'] as List<dynamic>)
        .map((e) => LeaderboardEntryModel.fromJson(e as Map<String, dynamic>))
        .toList();
  }
}