from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix, accuracy_score
import pandas as pd

def naive_bayes_from_csv(csv_path):
    dataset = pd.read_csv(csv_path)
    X = dataset.iloc[:, 0:-1].copy()
    y = dataset.iloc[:, -1]
    label_encoder = {}
    for column in X.columns:
        le = LabelEncoder()
        X[column] = le.fit_transform(X[column])
        label_encoder[column] = le
    target_encoder = LabelEncoder()
    y = target_encoder.fit_transform(y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
    model = GaussianNB()
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    return {
        'actual': y_test,
        'predicted': y_pred,
        'confusion_matrix': confusion_matrix(y_test, y_pred),
        'accuracy': accuracy_score(y_test, y_pred) * 100
    }
