export const  dbCreateCollection = async (newCollectionName, newCollectionType) => {
  console.log("dbCreateCollection");
  let result = await fetch(
    "http://127.0.0.1:5000/api/v1/create_collection",
    {
      method: "POST", // or 'PUT'
      headers: {
          "Content-Type": "application/json",
      },
      body: JSON.stringify(
          {
              "authenticate":{"username":"test_user","password":"CETSp@assword!"},
              "collection_info":{"collection_name": newCollectionName, "type":newCollectionType}
          }
      ),
    }
  )
  .then((response) => response.json())
  .then((response) => {
    if (response["status"] === "success") {
      console.log("dbCreateCollection Success")
      return response["collection_id"];
    }
  });


  return result
};

export const dbDeleteCollection = async (collectionName) => {
  console.log("dbDeleteCollection");
  const response = await fetch(
    "http://127.0.0.1:5000/api/v1/delete_collection",
    {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        "collection_info": { "collection_name": collectionName }
      }),
    }
  );

  const responseData = await response.json();
  if (responseData.status === "success") {
    console.log("dbDeleteCollection Success");
    return true;
  } else {
    console.error("dbDeleteCollection Failed");
    return false;
  }
};

export const dbGetCollection = async (collection_id) => {
  console.log("dbGetCollection");
  let result = await fetch("http://127.0.0.1:5000//api/v1/get_collection?collection=" + collection_id,)
  .then((response) => response.json())
  .then((response) => {
      console.log(response)
      if (response["status"] === "success") {
          return response["collection"]
      }
  });
  return result;
};

export const dbGetItems = async (collection_id) => {
  
  let result = await fetch(`http://127.0.0.1:5000//api/v1/get_items?collection=${collection_id}`)
  .then((response) => response.json())
  .then((response) => {
      console.log(response)
      if (response["status"] === "success") {
          return response["files"]
      }
  });
  console.log("dbGetItems",result);
  return result;
};

export const dbGetCollections = async (user_id) => {
  let result = await fetch("http://127.0.0.1:5000//api/v1/get_collections?user=" + user_id,)
  .then((response) => response.json())
  .then((response) => {
      console.log("dbGetCollections",response);
      if (response["status"] === "success") {
          return response["collections"]
      } else {
          return []
      }
  });
  return result;
};

export const dbUpdateCollectionGlob = async (collection_id,glob) => {
  console.log("dbUpdateCollection",collection_id);
  let result = await fetch(
      "http://127.0.0.1:5000/api/v1/update_collection_glob",
      {
        method: "POST", // or 'PUT'
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(
              {
                  "authenticate":{"username":"test_user","password":"CETSp@assword!"},
                  "collection_info":{
                      "collection_id": collection_id, 
                      "glob":glob
                  }
            }
        ),
      }
    )
    .then((response) => response.json())
    .then((response) => {
      if (response["status"] === "success") {
        return response;
      }
    });


    return result
};

export const dbUpdateCollectionSummary = async (collection_id, summarizer) => {
  console.log("dbUpdateCollectionSummary",collection_id);
  let result = await fetch(
      `http://127.0.0.1:5000/api/v1/summarize/${summarizer}`,
      {
        method: "POST", // or 'PUT'
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(
              {
                  "collection_info":{
                      "collection_id": collection_id, 
                  }
            }
        ),
      }
    )
    .then((response) => response.json())
    .then((response) => {
      if (response["status"] === "success") {
        return response;
      }
    });


    return result
};

export const dbUploadRawTextFiles = async (collection_id,formData) => {
  console.log("dbUpdateCollectionSummary",collection_id);

  let result = await fetch('http://127.0.0.1:5000//api/v1/upload_raw_text?collection='+collection_id, {
    // content-type header should not be specified!
      method: 'POST',
      body: formData,
    })
    .then(response => response.json())
    .then(success => {
      console.log(success)
      // Do something with the successful response
    })
    .catch(error => console.log(error)
  );

  return result
};

export const dbUploadRawHTMLFiles = async (collection_id,formData) => {
  console.log("dbUploadRawHTMLFiles",collection_id);

  let result = await fetch('http://127.0.0.1:5000//api/v1/upload_raw_html?collection='+collection_id, {
    // content-type header should not be specified!
      method: 'POST',
      body: formData,
    })
    .then(response => response.json())
    .then(success => {
      console.log(success)
      // Do something with the successful response
    })
    .catch(error => console.log(error)
  );

  return result
};

export const dbUploadRawHtmlFiles = async (collection_id,formData) => {
  console.log("dbUpdateCollectionSummary",collection_id);

  let result = await fetch('http://127.0.0.1:5000//api/v1/upload_raw_html?collection='+collection_id, {
    // content-type header should not be specified!
      method: 'POST',
      body: formData,
    })
    .then(response => response.json())
    .then(success => {
      console.log(success)
      // Do something with the successful response
    })
    .catch(error => console.log(error)
  );

  return result
};

export const dbUploadUrlFile = async (collection_id,formData) => {
  console.log("dbUpdateCollectionSummary",collection_id);

  let result = await fetch('http://127.0.0.1:5000//api/v1/upload_url_file?collection='+collection_id, {
    // content-type header should not be specified!
      method: 'POST',
      body: formData,
    })
    .then(response => response.json())
    .then(success => {
      console.log(success)
      // Do something with the successful response
    })
    .catch(error => console.log(error)
  );

  return result
};

export default dbCreateCollection;