/**
 * Communicates with the SKU prediction API to get a product SKU from an image.
 */

/**
 * Sends an image file to the prediction endpoint and returns a product SKU.
 *
 * @param imageFile The image file to be sent for prediction.
 * @returns A promise that resolves to the product SKU string if the prediction is successful, otherwise null.
 */
export async function getSkuFromImage(imageFile: File): Promise<string | null> {
  const formData = new FormData();
  formData.append("file", imageFile);

  try {
    const response = await fetch(
      "https://pegasus-working-bison.ngrok-free.app/predictfile",
      {
        method: "POST",
        body: formData,
      }
    );

    if (!response.ok) {
      console.error("SKU prediction API request failed:", response.status, response.statusText);
      return null;
    }

    const result = await response.json();

    if (result.status === true && typeof result.SKU === "string" && result.SKU) {
      return result.SKU;
    }

    return null;
  } catch (error) {
    console.error("Error calling SKU prediction API:", error);
    return null;
  }
}
