import api from "./api";

/* =========================================================
   TOKEN HELPERS
   ========================================================= */

const getPortalAccessToken = () => {
  return sessionStorage.getItem(
    "portalAccessToken"
  );
};

const getSubmissionToken = () => {
  return sessionStorage.getItem(
    "staffSubmissionToken"
  );
};

const getSubmissionHeaders = () => {
  const submissionToken =
    getSubmissionToken();

  if (!submissionToken) {
    throw new Error(
      "Your staff form session is missing. Please verify your Staff ID again."
    );
  }

  return {
    Authorization:
      `Bearer ${submissionToken}`
  };
};

/* =========================================================
   PUBLIC PORTAL
   ========================================================= */

export const getCurrentCampaign =
  async () => {
    const response =
      await api.get(
        "/public/campaign"
      );

    return response.data;
  };

export const validateGhanaCard =
  async ({
    ghana_card_number
  }) => {
    const response =
      await api.post(
        "/public/validate-ghana-card",
        {
          ghana_card_number
        }
      );

    return response.data;
  };

export const identifyStaff =
  async ({
    staff_number
  }) => {
    const verificationToken =
      sessionStorage.getItem(
        "ghanaCardVerificationToken"
      );

    if (!verificationToken) {
      throw new Error(
        "Your Ghana Card verification session is missing. Please start again."
      );
    }

    const response =
      await api.post(
        "/public/identify",
        {
          staff_number
        },
        {
          headers: {
            Authorization:
              `Bearer ${verificationToken}`
          }
        }
      );

    return response.data;
  };

export const getSubmissionSession =
  async () => {
    const response =
      await api.get(
        "/public/submission/session",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

/* =========================================================
   STAFF FORM
   ========================================================= */

export const getFormLookups =
  async () => {
    const response =
      await api.get(
        "/public/form/lookups",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const getFormProgress =
  async () => {
    const response =
      await api.get(
        "/public/form/progress",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const getPersonalDetails =
  async () => {
    const response =
      await api.get(
        "/public/form/personal-details",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const savePersonalDetails =
  async (
    personalDetails
  ) => {
    const response =
      await api.put(
        "/public/form/personal-details",
        personalDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };


  export const getEmploymentDetails =
  async () => {
    const response =
      await api.get(
        "/public/form/employment-details",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveEmploymentDetails =
  async (
    employmentDetails
  ) => {
    const response =
      await api.put(
        "/public/form/employment-details",
        employmentDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

  export const getNextOfKin =
  async () => {
    const response =
      await api.get(
        "/public/form/next-of-kin",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveNextOfKin =
  async (
    nextOfKinDetails
  ) => {
    const response =
      await api.put(
        "/public/form/next-of-kin",
        nextOfKinDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const getDependents =
  async () => {
    const response =
      await api.get(
        "/public/form/dependents",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveDependents =
  async (
    dependentDetails
  ) => {
    const response =
      await api.put(
        "/public/form/dependents",
        dependentDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

  export const getEmergencyContact =
  async () => {
    const response =
      await api.get(
        "/public/form/emergency-contact",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveEmergencyContact =
  async (
    emergencyContactDetails
  ) => {
    const response =
      await api.put(
        "/public/form/emergency-contact",
        emergencyContactDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

  export const getQualifications =
  async () => {
    const response =
      await api.get(
        "/public/form/qualifications",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveQualifications =
  async (
    qualificationDetails
  ) => {
    const response =
      await api.put(
        "/public/form/qualifications",
        qualificationDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

  export const getDocuments =
  async () => {
    const response =
      await api.get(
        "/public/form/documents",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const uploadDocument =
  async ({
    document_type,
    document_title,
    document
  }) => {
    if (!document) {
      throw new Error(
        "Select a document to upload."
      );
    }

    if (
      typeof File !==
        "undefined" &&
      !(document instanceof File)
    ) {
      throw new Error(
        "The selected document is not a valid file."
      );
    }

    const formData =
      new FormData();

    formData.append(
      "document_type",
      document_type
    );

    formData.append(
      "document_title",
      document_title || ""
    );

    /*
     * This field name must match:
     *
     * multer.single("document")
     */
    formData.append(
      "document",
      document,
      document.name
    );

    const response =
      await api.post(
        "/public/form/documents",
        formData,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };
export const deleteDocument =
  async (
    documentId
  ) => {
    const response =
      await api.delete(
        `/public/form/documents/${documentId}`,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const downloadDocument =
  async (
    documentId
  ) => {
    const response =
      await api.get(
        `/public/form/documents/${documentId}/download`,
        {
          headers:
            getSubmissionHeaders(),

          responseType:
            "blob"
        }
      );

    return response.data;
  };

export const saveDocumentsSection =
  async (
    documentSection
  ) => {
    const response =
      await api.put(
        "/public/form/documents/section",
        documentSection,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

  export const getDeclaration =
  async () => {
    const response =
      await api.get(
        "/public/form/declaration",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const saveDeclarationDraft =
  async (
    declarationDetails
  ) => {
    const response =
      await api.put(
        "/public/form/declaration",
        declarationDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const submitFinalDeclaration =
  async (
    declarationDetails
  ) => {
    const response =
      await api.post(
        "/public/form/declaration/submit",
        declarationDetails,
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };


  export const getCorrectionStatus =
  async () => {
    const response =
      await api.get(
        "/public/form/correction-status",
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const startCorrection =
  async () => {
    const response =
      await api.patch(
        "/public/form/correction/start",
        {},
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };

export const resubmitCorrection =
  async ({
    staff_response_note
  }) => {
    const response =
      await api.patch(
        "/public/form/correction/resubmit",
        {
          staff_response_note
        },
        {
          headers:
            getSubmissionHeaders()
        }
      );

    return response.data;
  };