/* eslint-disable no-console */
import Post from "@models/Feed";
import {
  APIPath,
} from "./backend";
import {
  authedGetBackendAPI, authedPostBackendAPI, authedDeleteBackendAPI, authedPutBackendAPI,
} from "./auth";

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

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

  static getFeed = async ({ limit, offset, auth = false }: Options = {}): Promise<Post[]> => {
    try {
      return await authedGetBackendAPI<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 authedPostBackendAPI<Post, Post>({ path: APIPath.FEED }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

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

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

export default FeedApi;
