/* eslint-disable no-console */
import axios from "axios";
import { Signup, SignupForm } from "@models/Signup";
import { getAuthHeader } from "@utils/auth";

export const URL = `${process.env.NEXT_PUBLIC_API_URL}/signup/`;
export const FORM_URL = `${process.env.NEXT_PUBLIC_API_URL}/signupForm/`;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Options {
  // onlyNonPast?: boolean;
  // limit?: number;
  // offset?: number;
  // auth?: boolean;
}

class SignupApi {
  static async getSignup(id: number): Promise<Signup> {
    try {
      const resp = await axios.get(`${URL}${id}`, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async createSignup(data: Signup): Promise<Signup> {
    try {
      const resp = await axios.post(URL, data);
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async updateSignup(data: Signup, uuid: string): Promise<Signup> {
    try {
      const { id } = data;
      if (!id) throw new Error("SignupId required!");
      const resp = await axios.put(`${URL}${id}/edit/`, data, {
        params: { uuid },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async getSignupUUID(id: number, uuid: string): Promise<Signup> {
    try {
      const resp = await axios.get(`${URL}${id}/edit/`, {
        params: {
          uuid,
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async deleteSignup(id: number) {
    try {
      const resp = await axios.delete(`${URL}${id}`, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async getForms(auth = false): Promise<SignupForm[]> {
    try {
      const headers = auth ? { Authorization: getAuthHeader() } : null;
      const resp = await axios.get(FORM_URL, {
        headers,
      });
      const { results } = resp.data;
      return results;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async getForm(id: number, auth = false): Promise<SignupForm> {
    try {
      const headers = auth ? { Authorization: getAuthHeader() } : null;
      const resp = await axios.get(`${FORM_URL}${id}/`, {
        headers,
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async createForm(data: SignupForm): Promise<SignupForm> {
    try {
      const resp = await axios.post(FORM_URL, data, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async updateForm(data: SignupForm): Promise<SignupForm> {
    try {
      const putUrl = `${FORM_URL}${data.id}/`;
      const resp = await axios.put(putUrl, data, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async deleteForm(id: number) {
    try {
      const resp = await axios.delete(`${FORM_URL}${id}`, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async signupFormSendEmail(data: any, id: number): Promise<any> {
    try {
      const resp = await axios.post(`${FORM_URL}${id}/sendemail/`, data, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }

  static async getSignups(id: number): Promise<Signup[]> {
    try {
      const resp = await axios.get(`${FORM_URL}${id}/signups/`, {
        headers: {
          Authorization: getAuthHeader(),
        },
      });
      return resp.data;
    } catch (err) {
      console.error(err);
      throw err;
    }
  }
}

export default SignupApi;
