import 'package:english_learning/core/services/dio_client.dart';
import 'package:english_learning/features/learning/modules/level/models/level_model.dart';

class LevelRepository {
  final DioClient _dioClient = DioClient();

  Future<Map<String, dynamic>> getLevels(String topicId, String token) async {
    try {
      final response = await _dioClient.getLevels(topicId, token);
      if (response.statusCode == 200 && response.data != null) {
        final Map<String, dynamic> responseData = response.data;
        if (responseData.containsKey('data')) {
          final Map<String, dynamic> data = responseData['data'] ?? {};
          final List<dynamic> levelsData = data['levels'] ?? [];
          final List<Level> levels =
              levelsData.map((json) => Level.fromJson(json)).toList();
          final Map<String, dynamic>? lastCompletedLevel =
              data['lastCompletedLevel'] ?? {};

          return {
            'levels': levels,
            'lastCompletedLevel': lastCompletedLevel,
          };
        } else {
          throw Exception('Invalid response structure: missing "data" key');
        }
      } else {
        throw Exception('Failed to load levels: ${response.statusCode}');
      }
    } catch (e) {
      print('Error in LevelRepository: $e');
      rethrow;
    }
  }
}
