import '../../models/group_model.dart';
import '../../models/group_member_model.dart';
import '../interfaces/group_service.dart';
import 'api_client.dart';

/// Real API implementation.
class GroupApiService implements GroupService {
  final ApiClient _client;

  GroupApiService(this._client);

  @override
  Future<List<GroupModel>> getGroups() async {
    final List<dynamic> data = await _client.get('/admin/groups');
    return data
        .map((json) => GroupModel.fromJson(json as Map<String, dynamic>))
        .toList();
  }

  @override
  Future<GroupModel> createGroup(String name) async {
    final data = await _client.post('/groups/', {'name': name});
    return GroupModel.fromJson(data as Map<String, dynamic>);
  }

  @override
  Future<void> inviteMember(String groupId, String username) async {
    await _client.post('/groups/$groupId/invite', {
      'username': username,
    });
  }

  @override
  Future<void> removeMember(String groupId, String userId) async {
    await _client.delete('/groups/$groupId/members/$userId');
  }

  @override
  Future<List<GroupMemberModel>> getGroupMembers(String groupId) async {
    final List<dynamic> data = await _client.get('/groups/$groupId/members');
    return data
        .map((json) => GroupMemberModel.fromJson(json as Map<String, dynamic>))
        .toList();
  }
}
