import 'package:dio/dio.dart'; import '../models/knowledge_item.dart'; import 'api_client.dart'; class KnowledgeApi { final Dio _dio; const KnowledgeApi(this._dio); /// Fetch a page of IDs. Returns (ids, total). Future<(List, int)> fetchIds({ String? noteType, List tags = const [], String sort = 'modified', String? q, int limit = 50, int offset = 0, }) async { try { final params = { 'limit': limit, 'offset': offset, 'sort': sort, if (noteType != null) 'type': noteType, if (tags.isNotEmpty) 'tags': tags.join(','), if (q != null && q.isNotEmpty) 'q': q, }; final response = await _dio.get('/api/knowledge/ids', queryParameters: params); final data = response.data as Map; final ids = (data['ids'] as List).map((e) => e as int).toList(); final total = data['total'] as int; return (ids, total); } on DioException catch (e) { throw dioToApp(e); } } /// Batch-hydrate up to 100 IDs into full items. Future> fetchBatch(List ids) async { if (ids.isEmpty) return []; try { final response = await _dio.get( '/api/knowledge/batch', queryParameters: {'ids': ids.join(',')}, ); final data = response.data as Map; return (data['items'] as List) .map((e) => KnowledgeItem.fromJson(e as Map)) .toList(); } on DioException catch (e) { throw dioToApp(e); } } /// Per-type counts for tab labels. Future> fetchCounts({List tags = const []}) async { try { final params = { if (tags.isNotEmpty) 'tags': tags.join(','), }; final response = await _dio.get('/api/knowledge/counts', queryParameters: params); return (response.data as Map) .map((k, v) => MapEntry(k, (v as num).toInt())); } on DioException catch (e) { throw dioToApp(e); } } /// All tags for the current type filter. Future> fetchTags({String? noteType}) async { try { final response = await _dio.get( '/api/knowledge/tags', queryParameters: {if (noteType != null) 'type': noteType}, ); return (response.data['tags'] as List) .map((e) => e as String) .toList(); } on DioException catch (e) { throw dioToApp(e); } } }