/* eslint-disable no-console */
import Post from "@models/Feed";
import {
  APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI,
} from "./backend";

interface Options {
  limit?: number;
  offset?: number;
  auth?: boolean;
}

class FeedApi {
  static getPost = async (id: number, auth?: boolean): Promise<Post> => {
    try {
      return await getBackendAPI<Post>({
        path: APIPath.FEED, urlParams: { id }, authenticated: auth,
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static getFeed = async ({ limit, offset, auth }: Options = {}): Promise<Post[]> => {
    try {
      return await getBackendAPI<Post[]>({
        path: APIPath.FEED,
        queryParams: {
          limit,
          offset,
        },
        authenticated: auth,
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static createPost = async (data: Post): Promise<Post> => {
    try {
      return await postBackendAPI<Post, Post>({ path: APIPath.FEED, authenticated: true }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static updatePost = async (data: Post): Promise<Post> => {
    try {
      return await putBackendAPI<Post, Post>({
        path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true,
      }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static deletePost = async (id: number): Promise<void> => {
    try {
      await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };
}

export default FeedApi;
