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

export type EmailRequest = {
  mode: "all" | "actual" | "reserve";
  subject: string;
  content: string;
};

class SignupApi {
  static getSignup = async (id: number): Promise<Signup> => {
    try {
      return await authedGetBackendAPI<Signup>({
        path: APIPath.SIGNUPS, urlParams: { id },
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static createSignup = async (data: Signup): Promise<Signup> => {
    try {
      return await postBackendAPI<Signup, Signup>({
        path: APIPath.SIGNUPS,
      }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static updateSignup = async (data: Signup, uuid: string): Promise<Signup> => {
    try {
      const { id } = data;
      if (!id) throw new Error("SignupId required!");
      return await authedPutBackendAPI<Signup, Signup>({
        path: APIPath.SIGNUPS_EDIT,
        urlParams: {
          id,
        },
        queryParams: {
          uuid,
        },
      }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static getSignupUUID = async (id: number, uuid: string): Promise<Signup> => {
    try {
      return await authedGetBackendAPI<Signup>({
        path: APIPath.SIGNUPS_EDIT,
        urlParams: {
          id,
        },
        queryParams: {
          uuid,
        },
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

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

  static getForm = async (id: number, auth = false): Promise<SignupForm> => {
    try {
      return await authedGetBackendAPI<SignupForm>({
        path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth,
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static getForms = async (auth = false): Promise<SignupForm[]> => {
    try {
      return await authedGetBackendAPI<SignupForm[]>({
        path: APIPath.SIGNUP_FORMS, authenticated: auth,
      });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static createForm = async (data: SignupForm): Promise<SignupForm> => {
    try {
      return await authedPostBackendAPI<SignupForm, SignupForm>({
        path: APIPath.SIGNUP_FORMS,
      }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

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

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

  static signupFormSendEmail = async (data: EmailRequest, id: number): Promise<void> => {
    try {
      await authedPostBackendAPI<EmailRequest, { message: "Email sent" }>({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id } }, data);
    } catch (err) {
      console.error(err);
      throw err;
    }
  };

  static getSignups = async (id: number): Promise<Signup[]> => {
    try {
      return await authedGetBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id } });
    } catch (err) {
      console.error(err);
      throw err;
    }
  };
}

export default SignupApi;
