import * as env from "../config/env.config";

// const APX_URL = "https://apx.didit.me";

// const getClientToken = async () => {
//   const url = APX_URL + "/auth/v2/token/";
//   const clientID = env.DIDIT_CLIENT_ID;
//   const clientSecret = env.DIDIT_CLIENT_SECRET;

//   const encodedCredentials = Buffer.from(
//     `${clientID}:${clientSecret}`
//   ).toString("base64");
//   const params = new URLSearchParams();
//   params.append("grant_type", "client_credentials");

//   try {
//     const response = await fetch(url, {
//       method: "POST",
//       headers: {
//         Authorization: `Basic ${encodedCredentials}`,
//         "Content-Type": "application/x-www-form-urlencoded",
//       },
//       body: params,
//     });

//     const data = await response.json();

//     if (response.ok) {
//       // Return the entire data object if you need to use other properties
//       return data;
//     } else {
//       console.error("Error fetching client token:", data.message);
//       return null;
//     }
//   } catch (error) {
//     console.error("Network error:", error);
//     return null;
//   }
// };

export const getSessionDecision = async (sessionId: string) => {
  const endpoint = `${env.DIDIT_VERIFICATION_URL}/session/${sessionId}/decision/`;
  // const token = await getClientToken();
  // if (!token) {
  //   console.error("Error fetching client token");
  // } else {

  // }

  const headers = {
    "Content-Type": "application/json",
    // Authorization: `Bearer ${token.access_token}`,
    "x-api-key": env.DIDIT_API_KEY,
  };

  try {
    const response = await fetch(endpoint, {
      method: "GET",
      headers,
    });

    const data = await response.json();

    if (response.ok) {
      return data;
    } else {
      throw new Error(data.message ?? data.detail);
    }
  } catch (err) {
    throw err;
  }
};

export const createSession = async (
  vendorData: string | null,
  metadata: string | null,
  callback: string | null
) => {
  const endpoint = `${env.DIDIT_VERIFICATION_URL}/session/`;

  const body = {
    vendor_data: vendorData,
    metadata: metadata,
    callback: callback,
    workflow_id: env.DIDIT_WORKFLOW_ID,
  };

  const headers = {
    "Content-Type": "application/json",
    "x-api-key": env.DIDIT_API_KEY,
  };

  try {
    const response = await fetch(endpoint, {
      method: "POST",
      headers,
      body: JSON.stringify(body),
    });

    const data = await response.json();

    if (response.ok) {
      return data;
    } else {
      throw new Error(data.message ?? data.detail);
    }
  } catch (err) {
    throw err;
  }
};

// export const getMaritalStatus = (maritalStatus: string) => {
//   switch (maritalStatus) {
//     case "SINGLE":
//       return "single";
//       break;
//     case "MARRIED":
//       return "married";
//       break;
//     case "DIVORCED":
//       return "divorced";
//       break;
//   }
// };
