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

use_srun = False
if platform == 'puhti' or platform == 'mahti':
    project = 'project_2006950'
    partition = 'small'
    with_containers = False
    puhti_module = 'pytorch/2.4'
    venv_path = '/projappl/project_2006950/syntheseus-python-10'
    container = None
elif platform == 'mahti':
    project = 'project_2006950'
    partition = 'small'
    with_containers = False
    puhti_module = 'pytorch/2.4'
    venv_path = '/projappl/project_2006950/syntheseus-python-10'
    container = None
elif platform == 'lumi':
    SCRIPT_DIR = 'scripts'
    platform = 'lumi'
    project = 'project_462000833'
    partition = 'small-g'
    with_containers = False
    venv_path = '/projappl/project_462000833/rxnmapper'
    puhti_module = 'pytorch/2.7'
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': '48:00:00',
    'partition': partition,
    'nodes': 1,
    'gpus-per-node': 1,
    'ntasks-per-node': 1,
    'cpus-per-task': 1,
    'mem': '100G', # 50G not enough for uspto_full
    'with_containers': with_containers,
    'use_srun': use_srun,
    'puhti_module': puhti_module,
    'venv_path': venv_path,
    'start_array_job': 0, # 5 to 37
    'end_array_job': 0 #37
}
subset = 'train_no_overlap_threshold100_dropped.csv'
data_dir = 'uspto_full/raw'
start_idx = 0
end_idx = 2000000
time_stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
script_args = {"script_dir": SCRIPT_DIR,
                "use_torchrun": 'false',
                "args": {
                    "reaction_dataset.subset": subset,
                    "reaction_dataset.data_dir": data_dir,
                    "reaction_dataset.start_idx": start_idx,
                    "reaction_dataset.end_idx": end_idx
                }
                }
script_args['script_name'] = 'prepare_file_for_rsmiles_training.py'
slurm_args['job_name'] = f'prepare_file_{subset}_{start_idx}_{end_idx}_{time_stamp}'
output = create_and_submit_batch_job(slurm_args, script_args, interactive=args.interactive)