import pandas as pd
import os
import zipfile
import json

from utils.cluster import cluster_data,plot_clusters_figure
from utils.myfile import test_function

zip_file_path = "bb231090-78d6-4271-b413-5adb6f8ab214/datafile.zip"
json_file_name = 'patient_data.json'
#Read data 
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    with zip_ref.open(json_file_name) as file:
        json_data = json.load(file)

data = pd.DataFrame(json_data)
result = test_function()
print(result)

#Code to run
#Add here the code to run. You can create different functions and the results should be clear stated.
clustered_data, reduced_clustered_data, cluster_centers = cluster_data(data=data)
clusters_figure = plot_clusters_figure(data=reduced_clustered_data, centroids=cluster_centers)

#Gather results
#The results variable should be a list of dicts. Each dict element must have the following elements:
# "data": the results, "name": name of the results (will be used a the filename in which this resuls will be stored),
# and "format": the format in which you want to store the results (fig, csv, txt for the moment)
filename="results/demofile2.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
f = open(filename, "w")
f.write("clustered_data: " + str(clustered_data) + "\n")
f.close()
