import os
import shap
import numpy as np


def compute_and_save_shap_values(model,
                                 X_train,
                                 X_test,
                                 output_directory,
                                 file_name):
    """
    Computes SHAP values for the provided model and saves them to the specified directory and file.

    Parameters:
    - model: The trained machine learning model.
    - X_train: The training data used to fit the SHAP explainer.
    - X_test: The test data used to compute SHAP values.
    - output_directory: The directory where the SHAP values will be saved.
    - file_name: The name of the file to save the SHAP values.
    """
    # Create the output directory if it doesn't exist
    os.makedirs(output_directory, exist_ok=True)

    # Compute SHAP values using the appropriate explainer
    explainer = shap.Explainer(model, X_train)
    shap_values = explainer(X_test)

    # Save the SHAP values to a .npy file
    shap_file_path = os.path.join(output_directory, f"{file_name}.npy")
    np.save(shap_file_path, shap_values.values)

    print(f"SHAP values saved to {shap_file_path}")
