from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
from sklearn import svm
from pandas import read_csv
import numpy as np 
import subprocess

# define a Gaussain NB classifier
clf = GaussianNB()

# define the class encodings and reverse encodings
classes = {0: False, 1: True}
r_classes = {y: x for x, y in classes.items()}

# function to train and load the model during startup
def load_model():
    # loading dataframe from bug_pred.csv file
    df = read_csv("bug_pred.csv")
    X = df.drop("defects",axis=1)
    y = df["defects"]

    # do the test-train split and train the model
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    clf.fit(X_train, y_train)

    # calculate the print the accuracy score
    acc = accuracy_score(y_test, clf.predict(X_test))
    print(f"Model trained with accuracy: {round(acc, 3)}")

    # subprocess.call(["jupyter","nbconvert","--to","notebook","--inplace","--execute","dataset/h2o_explainability.ipynb"],shell=True)
    # subprocess.call(["jupyter","nbconvert","--execute","--to","html","dataset/h2o_explainability.ipynb"],shell=True)


# function to predict the if its a bug using the model
def predict(query_data):
    x = list(query_data.dict().values())
    prediction = clf.predict([x])[0]
    print(f"Model prediction: {classes[prediction]}")
    return classes[prediction]

# function to retrain the model as part of the feedback loop
def retrain(data):
    # pull out the relevant X and y from the FeedbackIn object
    X = [list(d.dict().values())[:-1] for d in data]
    y = [r_classes[d.defects] for d in data]

    # fitting the classifier again based on the new data 
    clf.fit(X, y)
