import { handleApiRequest } from 'Utils/handleApiRequest';
import { Api } from 'Utils/api';
import { FormRequestBody, FormTemplateResponse } from 'Containers/Forms/Models';

export const CONTRACT_FORMS = 'CONTRACT_FORMS';
export const FETCHING_CONTRACT_FORMS = 'FETCHING_CONTRACT_FORMS';
export const DELETING_CONTRACT_FORM = 'DELETING_CONTRACT_FORM';
export const CONTRACT_FORM_DELETED = 'CONTRACT_FORM_DELETED';
export const ADDING_CONTRACT_FORM = 'ADDING_CONTRACT_FORM';
export const CONTRACT_FORM_ADDED = 'CONTRACT_FORM_ADDED';
export const EDITING_CONTRACT_FORM = 'EDITING_CONTRACT_FORM';
export const CONTRACT_FORM_EDITED = 'CONTRACT_FORM_EDITED';
export const CONTRACT_FORM_ERRORS = 'CONTRACT_FORM_ERRORS';

interface FormsActionTypes {
  CONTRACT_FORMS: FormTemplateResponse;
  FETCHING_CONTRACT_FORMS: boolean;
  DELETING_CONTRACT_FORM: boolean;
  CONTRACT_FORM_DELETED: number;
  ADDING_CONTRACT_FORM: boolean;
  CONTRACT_FORM_ADDED: boolean;
  EDITING_CONTRACT_FORM: boolean;
  CONTRACT_FORM_EDITED: boolean;
  CONTRACT_FORM_ERRORS: any;
}

interface MessageAction {
  type: keyof FormsActionTypes;
  payload: any;
}

export type SetFormsActionTypes = MessageAction;

// Thunk to fetch contract forms for a company
export const listCompanyContractForms = (companyId: number) => async (dispatch: any) => {
  const response = await handleApiRequest(
    dispatch,
    Api.get(`companies/${companyId}/contract-forms`),
    '',
    FETCHING_CONTRACT_FORMS
  );

  dispatch({
    type: CONTRACT_FORMS,
    payload: response,
  });
};

export const setDeletedFormId = (value: number | null) => async (dispatch: any) => {
  dispatch({ type: CONTRACT_FORM_DELETED, payload: value });
};

// Thunk to delete a contract form by id
export const deleteContractForm = (contractFormId: number) => async (dispatch: any) => {
  dispatch({ type: DELETING_CONTRACT_FORM, payload: true });
  try {
    await handleApiRequest(dispatch, Api.delete(`/contract-forms/${contractFormId}`), '', DELETING_CONTRACT_FORM);
    dispatch(setDeletedFormId(contractFormId));
    // dispatch({ type: CONTRACT_FORM_DELETED, payload: contractFormId });
  } catch (error) {
    // handle error if needed
    dispatch({ type: DELETING_CONTRACT_FORM, payload: false });
  }
};

export const setContractFormAdded = (value: boolean) => (dispatch: any) => {
  dispatch({
    type: CONTRACT_FORM_ADDED,
    payload: value,
  });
};

// Thunk to add a contract form
export const addContractForm = (formData: FormRequestBody) => async (dispatch: any) => {
  dispatch({ type: ADDING_CONTRACT_FORM, payload: true });
  try {
    await handleApiRequest(
      dispatch,
      Api.post('/contract-forms', formData),
      'CONTRACT_FORM_ERRORS',
      ADDING_CONTRACT_FORM
    );
    dispatch(setContractFormAdded(true));
  } catch (error) {
    dispatch({ type: ADDING_CONTRACT_FORM, payload: false });
    dispatch(setContractFormAdded(false));
  }
};

export const setContractFormEdited = (value: boolean) => (dispatch: any) => {
  dispatch({
    type: CONTRACT_FORM_EDITED,
    payload: value,
  });
};

// Thunk to edit a contract form
export const editContractForm = (formData: FormRequestBody, contractId: string) => async (dispatch: any) => {
  dispatch({ type: EDITING_CONTRACT_FORM, payload: true });
  try {
    await handleApiRequest(
      dispatch,
      Api.put('/contract-forms', formData),
      'CONTRACT_FORM_ERRORS',
      EDITING_CONTRACT_FORM
    );
    dispatch(setContractFormEdited(true));
  } catch (error) {
    dispatch({ type: EDITING_CONTRACT_FORM, payload: false });
    dispatch(setContractFormEdited(false));
  }
};

export const setContractFormErrors = (errors: any) => (dispatch: any) => {
  dispatch({
    type: CONTRACT_FORM_ERRORS,
    payload: errors,
  });
};
