from slurm_utils import create_and_submit_batch_job
from datetime import datetime
import os
from pathlib import Path
import argparse

PROJECT_ROOT = Path(os.path.realpath(__file__)).parents[1]

parser = argparse.ArgumentParser()
parser.add_argument('--interactive', action='store_true')
parser.add_argument('--platform', type=str)
args = parser.parse_args()
# DATA_DIR = 'uspto_full'
SCRIPT_DIR = 'scripts'
platform = args.platform

if platform == 'puhti' or platform == 'mahti':
    project = 'project_2007775'
    partition = 'small'
    with_containers = False
    puhti_module = 'pytorch/2.4'
    venv_path = '/projappl/project_2007775/syntheseus-python-10'
    container = None
elif platform == 'mahti':
    project = 'project_2007775'
    partition = 'small'
    with_containers = False
    puhti_module = 'pytorch/2.4'
    venv_path = '/projappl/project_2007775/syntheseus-python-10'
    container = None
elif platform == 'lumi':
    SCRIPT_DIR = 'scripts'
    platform = 'lumi'
    project = 'project_462000833'
    partition = 'small'
    with_containers = True
    container = 'multiguide-lumi.sif'
    venv_path = 'multiguide-lumi-container'
    puhti_module = None
else:
    raise ValueError(f'Platform {platform} not supported')

slurm_args = {
    'job_dir': 'jobs',
    'job_ids_file': 'job_ids.txt',
    'output_dir': 'output',
    'platform': platform,
    'project': project,
    'time': '06:00:00',
    'partition': partition,
    'nodes': 1,
    'gpus-per-node': 0,
    'ntasks-per-node': 1,
    'cpus-per-task': 1,
    'mem': '100G', # 50G not enough for uspto_full
    'with_containers': True,
    'container': 'multiguide-lumi.sif',
    'venv_path': 'multiguide-lumi-container',
    'start_array_job': 0, # 5 to 37
    'end_array_job': 99 #37
}
time_stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
script_args = {"script_dir": SCRIPT_DIR,
                "use_torchrun": 'false',
                "args": {
                    "route_dataset": "uspto_190",
                    "route_dataset.start_idx": "$start_idx",
                    "route_dataset.end_idx": "$end_idx",
                    "route_dataset.subset": "train.json"
                },
                "variables": {'targets_per_job': 1000, # 5 molecules per job in array
                              'offset': 210000,
                              'start_idx': '$((offset+(SLURM_ARRAY_TASK_ID * targets_per_job)))',
                              'end_idx': '$((start_idx+targets_per_job))'
                }
                }
script_args['script_name'] = 'get_reactions_with_classes_from_routes.py'
slurm_args['job_name'] = f'get_reactions_with_classes_from_routes_{time_stamp}'
output = create_and_submit_batch_job(slurm_args, script_args, interactive=args.interactive)