import 'dart:convert'; import 'package:http/http.dart' as http; import '../../config/api_config.dart'; import '../../models/question_source_preference.dart'; import '../../services/auth_service.dart'; import 'question_source_api_exception.dart'; /// HTTP client for per-user question source preferences. class QuestionSourceRemoteStore { QuestionSourceRemoteStore({http.Client? client}) : _client = client ?? http.Client(); final http.Client _client; Future?> fetchPreferences() async { final String? token = await AuthService.instance.getIdToken(); if (token == null) { return null; } final http.Response response = await _client.get( Uri.parse('$apiBaseUrl/v1/me/question-sources'), headers: _authHeaders(token), ); if (response.statusCode != 200) { throw QuestionSourceApiException(response.statusCode, response.body); } final Map body = jsonDecode(response.body) as Map; final List sources = body['sources'] as List? ?? []; return sources .map( (dynamic entry) => QuestionSourcePreference.fromJson( entry as Map, ), ) .toList(); } Future> savePreferences( List sources, ) async { final String? token = await AuthService.instance.getIdToken(); if (token == null) { throw StateError('Not signed in'); } final http.Response response = await _client.put( Uri.parse('$apiBaseUrl/v1/me/question-sources'), headers: _authHeaders(token), body: jsonEncode({ 'sources': sources .map((QuestionSourcePreference source) => source.toStorageJson()) .toList(), }), ); if (response.statusCode != 200) { throw QuestionSourceApiException(response.statusCode, response.body); } final Map body = jsonDecode(response.body) as Map; final List saved = body['sources'] as List? ?? []; return saved .map( (dynamic entry) => QuestionSourcePreference.fromJson( entry as Map, ), ) .toList(); } Map _authHeaders(String token) { return { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', 'Accept': 'application/json', }; } }