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

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

class EventApi {
  static getEvent = async (id: number, auth = false): Promise<Event> => {
    try {
      return await getBackendAPI<Event>({
        path: APIPath.EVENTS, urlParams: { id }, authenticated: auth,
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

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

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

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

  static deleteEvent = 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 EventApi;
