import 'package:dio/dio.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../models/context_member_model.dart'; import '../models/context_model.dart'; import '../models/message_model.dart'; import '../models/profile_model.dart'; import '../models/room_model.dart'; import 'api_client.dart'; final msnApiProvider = Provider((ref) => MsnApi(ref.watch(dioProvider))); class MsnApi { MsnApi(this._dio); final Dio _dio; Future> listContexts() async { final res = await _dio.get>('/api/contexts'); final list = res.data!['contexts'] as List; return list.map((e) => ContextModel.fromJson(e as Map)).toList(); } Future createContext({required String name, String kind = 'work'}) async { final res = await _dio.post>( '/api/contexts', data: {'name': name, 'kind': kind}, ); final d = res.data!; return ContextModel( id: d['id'] as String, name: d['name'] as String, kind: d['kind'] as String, ); } Future inviteToContext(String contextId, String email) async { await _dio.post('/api/contexts/$contextId/members', data: {'email': email}); } Future> listContextMembers(String contextId) async { final res = await _dio.get>('/api/contexts/$contextId/members'); final list = res.data!['members'] as List? ?? []; return list.map((e) => ContextMember.fromJson(e as Map)).toList(); } Future getMyProfile(String contextId) async { final res = await _dio.get>( '/api/profiles/me', queryParameters: {'contextId': contextId}, ); return ProfileModel.fromJson(res.data!); } Future getUserProfile(String contextId, String userId) async { final res = await _dio.get>( '/api/profiles/user/$userId', queryParameters: {'contextId': contextId}, ); return ProfileModel.fromJson(res.data!); } Future updateMyProfile( String contextId, { String? displayName, String? statusMessage, }) async { await _dio.patch( '/api/profiles/me', queryParameters: {'contextId': contextId}, data: { if (displayName != null) 'displayName': displayName, if (statusMessage != null) 'statusMessage': statusMessage, }, ); } Future> listRooms(String contextId) async { final res = await _dio.get>( '/api/rooms', queryParameters: {'contextId': contextId}, ); final list = res.data!['rooms'] as List? ?? []; return list.map((e) => RoomModel.fromJson(e as Map)).toList(); } Future openDirectRoom(String contextId, String otherUserId) async { final res = await _dio.post>( '/api/rooms/direct', data: {'contextId': contextId, 'otherUserId': otherUserId}, ); return res.data!['roomId'] as String; } Future> fetchMessages(String roomId, {String? before}) async { final res = await _dio.get>( '/api/messages', queryParameters: { 'roomId': roomId, 'limit': 50, if (before != null) 'before': before, }, ); final list = res.data!['messages'] as List? ?? []; return list.map((e) => MessageModel.fromJson(e as Map)).toList(); } Future registerPushToken(String token, {String platform = 'fcm'}) async { await _dio.post('/api/push/register', data: {'token': token, 'platform': platform}); } Future createGroupRoom(String contextId, String name, List memberIds) async { final res = await _dio.post>( '/api/rooms/group', data: {'contextId': contextId, 'name': name, 'memberIds': memberIds}, ); return res.data!['roomId'] as String; } Future>> searchMessages(String contextId, String q) async { final res = await _dio.get>( '/api/messages/search', queryParameters: {'contextId': contextId, 'q': q}, ); final list = res.data!['results'] as List? ?? []; return list.cast>(); } /// Latest message id read by the other participant in a 1:1 room (null if none / group). Future getReadState(String roomId) async { final res = await _dio.get>( '/api/messages/read-state', queryParameters: {'roomId': roomId}, ); return res.data!['lastReadMessageId'] as String?; } }