import 'dart:convert'; import '../models/post_models.dart'; import '../models/comment_models.dart'; import 'http_service.dart'; import 'auth_service.dart'; class PostService { static Future> getAllPosts() async { try { final response = await HttpService.get('/posts/all'); if (response.statusCode == 200) { final responseData = jsonDecode(response.body); return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'posts': (responseData['data'] as List?) ?.map((post) => Post.fromJson(post)) .toList() ?? [], }; } else if (response.statusCode == 401 || response.statusCode == 403) { await AuthService.handleAuthenticationError(); return { 'success': false, 'message': 'Session expired. Please login again.', 'posts': [], }; } else { try { final responseData = jsonDecode(response.body); return { 'success': false, 'message': responseData['message'] ?? 'Failed to fetch posts', 'posts': [], }; } catch (e) { return { 'success': false, 'message': 'Server error (${response.statusCode})', 'posts': [], }; } } } catch (e) { print('Error fetching posts: $e'); return { 'success': false, 'message': 'Network error: $e', 'posts': [], }; } } static Future> getUserPosts() async { try { final response = await HttpService.get('/posts/user'); final responseData = jsonDecode(response.body); if (response.statusCode == 200) { return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'posts': (responseData['data'] as List?)?.map((post) => Post.fromJson(post)).toList() ?? [], }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to fetch user posts', 'posts': [], }; } } catch (e) { print('Error fetching user posts: $e'); return { 'success': false, 'message': 'Network error: $e', 'posts': [], }; } } static Future> createPost(String body) async { try { final createPostRequest = CreatePostRequest(body: body); final response = await HttpService.post( '/posts/create', createPostRequest.toJson(), ); final responseData = jsonDecode(response.body); if (response.statusCode == 200 || response.statusCode == 201) { return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'post': responseData['data'] != null ? Post.fromJson(responseData['data']) : null, }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to create post', 'post': null, }; } } catch (e) { print('Error creating post: $e'); return {'success': false, 'message': 'Network error: $e', 'post': null}; } } static Future> likePost(String postId) async { try { final response = await HttpService.post( '/posts/like', {'postId': postId}, ); final responseData = jsonDecode(response.body); if (response.statusCode == 200) { return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'post': responseData['data'] != null ? Post.fromJson(responseData['data']) : null, }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to like post', 'post': null, }; } } catch (e) { print('Error liking post: $e'); return {'success': false, 'message': 'Network error: $e', 'post': null}; } } static Future> unlikePost(String postId) async { try { final response = await HttpService.post( '/posts/unlike', {'postId': postId}, ); final responseData = jsonDecode(response.body); if (response.statusCode == 200) { return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'post': responseData['data'] != null ? Post.fromJson(responseData['data']) : null, }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to unlike post', 'post': null, }; } } catch (e) { print('Error unliking post: $e'); return {'success': false, 'message': 'Network error: $e', 'post': null}; } } static Future> getPost(String postId) async { try { final response = await HttpService.get('/posts/get?id=$postId'); final responseData = jsonDecode(response.body); if (response.statusCode == 200) { return { 'success': responseData['status'] ?? false, 'message': responseData['message'] ?? '', 'post': responseData['data'] != null ? DetailedPost.fromJson(responseData['data']) : null, }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to fetch post', 'post': null, }; } } catch (e) { print('Error fetching post: $e'); return {'success': false, 'message': 'Network error: $e', 'post': null}; } } static Future> getComments(String postId) async { try { final response = await HttpService.get('/posts/comments/list?postId=$postId'); if (response.statusCode == 200) { final responseData = jsonDecode(response.body); final commentsResponse = CommentsResponse.fromJson(responseData); if (commentsResponse.status) { return { 'success': true, 'comments': commentsResponse.data, }; } else { return { 'success': false, 'message': commentsResponse.message ?? 'Failed to fetch comments', 'comments': [], }; } } else if (response.statusCode == 401 || response.statusCode == 403) { await AuthService.handleAuthenticationError(); return { 'success': false, 'message': 'Session expired. Please login again.', 'comments': [], }; } else { try { final responseData = jsonDecode(response.body); return { 'success': false, 'message': responseData['message'] ?? 'Failed to fetch comments', 'comments': [], }; } catch (e) { return { 'success': false, 'message': 'Server error (${response.statusCode})', 'comments': [], }; } } } catch (e) { print('Error fetching comments: $e'); return { 'success': false, 'message': 'Network error: $e', 'comments': [], }; } } static Future> createComment({ required String postId, required String body, String? replyComment, }) async { try { final requestBody = { 'postId': int.parse(postId), 'body': body, }; if (replyComment != null) { requestBody['replyComment'] = int.parse(replyComment); } final response = await HttpService.post('/posts/comments/create', requestBody); if (response.statusCode == 200 || response.statusCode == 201) { final responseData = jsonDecode(response.body); if (responseData['status'] == true) { return { 'success': true, 'comment': Comment.fromJson(responseData['data']), }; } else { return { 'success': false, 'message': responseData['message'] ?? 'Failed to create comment', }; } } else if (response.statusCode == 401 || response.statusCode == 403) { await AuthService.handleAuthenticationError(); return { 'success': false, 'message': 'Session expired. Please login again.', }; } else { try { final responseData = jsonDecode(response.body); return { 'success': false, 'message': responseData['message'] ?? 'Failed to create comment', }; } catch (e) { return { 'success': false, 'message': 'Server error (${response.statusCode})', }; } } } catch (e) { print('Error creating comment: $e'); return { 'success': false, 'message': 'Network error: $e', }; } } }