import 'dart:convert';
import '../models/post_models.dart';
import 'http_service.dart';

class PostService {
  static Future<Map<String, dynamic>> getAllPosts() async {
    try {
      final response = await HttpService.get('/posts/all');

      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 posts',
          'posts': <Post>[],
        };
      }
    } catch (e) {
      print('Error fetching posts: $e');
      return {
        'success': false,
        'message': 'Network error: $e',
        'posts': <Post>[],
      };
    }
  }

  static Future<Map<String, dynamic>> 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': <Post>[],
        };
      }
    } catch (e) {
      print('Error fetching user posts: $e');
      return {
        'success': false,
        'message': 'Network error: $e',
        'posts': <Post>[],
      };
    }
  }

  static Future<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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};
    }
  }
}
