import pickle
import numpy as np
import json
import sys
import os
import xgboost as xgb 

base_path = os.path.dirname(os.path.abspath(__file__))
parent_path = os.path.abspath(os.path.join(base_path, '..', '..'))
model_path = os.path.join(parent_path, 'storage', 'app', 'models', 'Model_SKLearn_XGB_VARK.pkl')
encoder_path = os.path.join(parent_path, 'storage', 'app', 'models', 'Encoder_Result.pkl')

with open(model_path, 'rb') as f:
    model = pickle.load(f)

with open(encoder_path, 'rb') as f:
    le_result = pickle.load(f)

input_data = json.loads(sys.argv[1])
visual = input_data['Visual']
auditory = input_data['Auditory']
read_write = input_data['Read/Write']
kinesthetic = input_data['Kinesthetic']

X = np.array([visual, auditory, read_write, kinesthetic]).reshape(1, -1)
feature_names = ['Visual', 'Auditory', 'Read/Write', 'Kinesthetic']
X_dmatrix = xgb.DMatrix(X, feature_names=feature_names)

pred = model.predict(X_dmatrix)
result = le_result.inverse_transform([int(pred[0])])[0]

output = {
    'Result': result,
    'Visual': visual,
    'Auditory': auditory,
    'Read/Write': read_write,
    'Kinesthetic': kinesthetic
}
print(json.dumps(output))