import 'package:flutter/material.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_response_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/create_space_template_body_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_body_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.dart'; import 'package:syncrow_web/services/api/http_service.dart'; import 'package:syncrow_web/utils/constants/api_const.dart'; import 'package:syncrow_web/utils/constants/temp_const.dart'; class CommunitySpaceManagementApi { // Community Management APIs Future> fetchCommunities(String projectId, {int page = 1}) async { try { List allCommunities = []; bool hasNext = true; while (hasNext) { await HTTPService().get( path: ApiEndpoints.getCommunityList .replaceAll('{projectId}', projectId), queryParameters: {'page': page}, expectedResponseModel: (json) { try { List jsonData = json['data'] ?? []; hasNext = json['hasNext'] ?? false; int currentPage = json['page'] ?? 1; List communityList = jsonData.map((jsonItem) { return CommunityModel.fromJson(jsonItem); }).toList(); allCommunities.addAll(communityList); page = currentPage + 1; return communityList; } catch (_) { hasNext = false; return []; } }, ); } return allCommunities; } catch (e) { return []; } } Future getCommunityById(String communityId) async { try { final response = await HTTPService().get( path: ApiEndpoints.getCommunityById .replaceAll('{communityId}', communityId), expectedResponseModel: (json) { return CommunityModel.fromJson(json['data']); }, ); return response; } catch (e) { debugPrint('Error fetching community by ID: $e'); return null; } } Future createCommunity( String name, String description, String projectId) async { try { final response = await HTTPService().post( path: ApiEndpoints.createCommunity.replaceAll('{projectId}', projectId), body: { 'name': name, 'description': description, }, expectedResponseModel: (json) { return CommunityModel.fromJson(json['data']); }, ); return response; } catch (e) { debugPrint('Error creating community: $e'); return null; } } Future updateCommunity( String communityId, String name, String projectId) async { try { final response = await HTTPService().put( path: ApiEndpoints.updateCommunity .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), body: { 'name': name, }, expectedResponseModel: (json) { return json['success'] ?? false; }, ); return response; } catch (e) { debugPrint('Error updating community: $e'); return false; } } Future deleteCommunity(String communityId, String projectId) async { try { final response = await HTTPService().delete( path: ApiEndpoints.deleteCommunity .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { return json['success'] ?? false; }, ); return response; } catch (e) { debugPrint('Error deleting community: $e'); return false; } } Future fetchSpaces( String communityId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.listSpaces .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { return SpacesResponse.fromJson(json); }, ); return response; } catch (e) { debugPrint('Error fetching spaces: $e'); return SpacesResponse( data: [], message: 'Error fetching spaces', page: 1, size: 10, totalItem: 0, totalPage: 0, hasNext: false, hasPrevious: false, ); } } Future getSpace( String communityId, String spaceId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.getSpace .replaceAll('{communityId}', communityId) .replaceAll('{spaceId}', spaceId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { return SpaceModel.fromJson(json['data']); }, ); return response; } catch (e) { debugPrint('Error fetching space: $e'); return null; // Assuming an empty SpaceModel constructor } } Future createSpace( {required String communityId, required String name, String? parentId, String? direction, bool isPrivate = false, required Offset position, String? spaceModelUuid, String? icon, List? tags, List? subspaces, required String projectId}) async { try { final body = { 'spaceName': name, 'isPrivate': isPrivate, 'x': position.dx, 'y': position.dy, 'direction': direction, 'icon': icon, }; if (parentId != null) { body['parentUuid'] = parentId; } if (tags != null) { body['tags'] = tags; } if (spaceModelUuid != null) body['spaceModelUuid'] = spaceModelUuid; if (subspaces != null) body['subspaces'] = subspaces; final response = await HTTPService().post( path: ApiEndpoints.createSpace .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), body: body, expectedResponseModel: (json) { return SpaceModel.fromJson(json['data']); }, ); return response; } catch (e) { debugPrint('Error creating space: $e'); return null; } } Future updateSpace( {required String communityId, required spaceId, required String name, String? parentId, String? icon, String? direction, bool isPrivate = false, required Offset position, List? tags, List? subspaces, String? spaceModelUuid, required String projectId}) async { try { final body = { 'spaceName': name, 'isPrivate': isPrivate, 'x': position.dx, 'y': position.dy, 'direction': direction, 'icon': icon, 'subspace': subspaces, 'tags': tags, 'spaceModelUuid': spaceModelUuid, }; if (parentId != null) { body['parentUuid'] = parentId; } final response = await HTTPService().put( path: ApiEndpoints.updateSpace .replaceAll('{communityId}', communityId) .replaceAll('{spaceId}', spaceId) .replaceAll('{projectId}', projectId), body: body, expectedResponseModel: (json) { return json['success'] ?? false; }, ); return response; } catch (e) { debugPrint('Error updating space: $e'); return false; } } Future deleteSpace( String communityId, String spaceId, String projectId) async { try { final response = await HTTPService().delete( path: ApiEndpoints.deleteSpace .replaceAll('{communityId}', communityId) .replaceAll('{spaceId}', spaceId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { return json['success'] ?? false; }, ); return response; } catch (e) { debugPrint('Error deleting space: $e'); return false; } } Future> getSpaceHierarchy( String communityId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.getSpaceHierarchy .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { final spaceModels = (json['data'] as List) .map((spaceJson) => SpaceModel.fromJson(spaceJson)) .toList(); return spaceModels; }, ); return response; } catch (e) { debugPrint('Error fetching space hierarchy: $e'); return []; } } }