from pathlib import Path
from stereograph.data import read_table, preprocess_data, save_graphs
from scripts.make_graphs import make_graphs
from stereograph.config import load_config
import argparse


def main(filename, config_file, graph_path, overwrite=False):
    if Path(graph_path).exists() and not overwrite:
        raise FileExistsError(f"Graph data already exists at {graph_path}. Use --overwrite to overwrite.")
        
    config = load_config(config_file)
    data = read_table(filename, tel_ids=config["data"]["tel_ids"], stop=config["data"]["max_events"])
    data = preprocess_data(data, training_features=config["data"]["input_features"])
    graph_data_list = make_graphs(data, config["data"]["input_features"])

    save_graphs(graph_data_list, config, graph_path, overwrite=overwrite)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Produce graphs from an hdf5 data file')
    parser.add_argument('--filename', '-f', type=Path, help='Path to the data file')
    parser.add_argument('--config_file', '-c', type=Path, help='Path to the config file')
    parser.add_argument('--output', '-o', type=Path, help='Path to save the graph data')
    parser.add_argument('--overwrite', action='store_true', help='Overwrite the output graph data if it exists')

    args = parser.parse_args()

    main(args.filename, args.config_file, args.output, args.overwrite)

