path_to_monofonic_binary = "/home/aubin/monofonic/build/monofonIC"

def main_monofonic(parsed_args):
    from parameters_monofonic import main_parameters_monofonic
    from low_level import print_starting_module, print_message, print_ending_module
    from os.path import isfile
    import subprocess
    
    print_starting_module("monofonic", verbose=parsed_args.verbose)
    monofonic_dict=main_parameters_monofonic(parsed_args)

    if parsed_args.execution == "local":
        print_message("Running monofonic in local mode.", 1, "monofonic", verbose=parsed_args.verbose)

        if not isfile(path_to_monofonic_binary):
            raise FileNotFoundError(f"Monofonic binary not found at {path_to_monofonic_binary}.")
        
        command_args = [path_to_monofonic_binary, monofonic_dict["config"]]

        if parsed_args.verbose < 2:
            subprocess.run(command_args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        else:
            subprocess.run(command_args)

        print_message("Monofonic finished.", 1, "monofonic", verbose=parsed_args.verbose)
    
    elif parsed_args.execution == "slurm":
        from slurm_submission import create_slurm_script, parse_arguments_slurm
        from args_main import parse_arguments_main
        print_message("Running monofonic in slurm mode.", 1, "monofonic", verbose=parsed_args.verbose)
        slurm_dict=parse_arguments_slurm(parsed_args)
        main_dict=parse_arguments_main(parsed_args)
        slurm_script = slurm_dict["scripts"]+"monofonic.sh"

        if not isfile(slurm_script) or parsed_args.force:
            print_message(f"SLURM script {slurm_script} does not exist (or forced). Creating it.", 2, "monofonic", verbose=parsed_args.verbose)
            create_slurm_script(
                slurm_template=slurm_dict["monofonic_template"],
                slurm_script=slurm_script,
                job="monofonic",
                job_config_file=monofonic_dict["config"],
                job_log=main_dict["logdir"]+"monofonic.log",
            )
            print_message(f"SLURM script written to {slurm_script}.", 3, "monofonic", verbose=parsed_args.verbose)
        else:
            print_message(f"SLURM script {slurm_script} exists.", 2, "monofonic", verbose=parsed_args.verbose)
        
        print_message(f"Submitting monofonic job to SLURM.", 1, "monofonic", verbose=parsed_args.verbose)

        command_args = ["sbatch", slurm_script]

        if parsed_args.verbose < 2:
            subprocess.run(command_args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        else:
            subprocess.run(command_args)

        print_message("Monofonic job submitted.", 2, "monofonic", verbose=parsed_args.verbose)
    
    else:
        raise ValueError(f"Execution mode {parsed_args.execution} not recognized.")
    
    print_ending_module("monofonic", verbose=parsed_args.verbose)



if __name__ == "__main__":
    from argparse import ArgumentParser
    from parameters_monofonic import register_arguments_monofonic
    from args_main import register_arguments_main
    from parameters_card import register_arguments_card_for_ICs
    from cosmo_params import register_arguments_cosmo
    from slurm_submission import register_arguments_slurm

    parser = ArgumentParser(description="Run monofonIC initial conditions generator for a Simbelmyne simulation.")
    # TODO: reduce the volume of arguments 
    register_arguments_main(parser)
    register_arguments_monofonic(parser)
    register_arguments_slurm(parser)
    register_arguments_card_for_ICs(parser)
    register_arguments_cosmo(parser)
    parsed_args = parser.parse_args()

    main_monofonic(parsed_args)