import os
import shutil
from pathlib import Path

#model = "hh_model_bbtt__model_default_run3"
model = "hh_model__model_default"

def sync_folders(source_path: str, destination_path: str):
    source = Path(source_path).resolve()
    destination = Path(destination_path).resolve()

    if not source.exists():
        raise FileNotFoundError(f"Source folder not found: {source}")

    if not destination.exists():
        destination.mkdir(parents=True)

    for root, dirs, files in os.walk(source):
        rel_path = Path(root).relative_to(source)
        dest_dir = destination / rel_path
        dest_dir.mkdir(parents=True, exist_ok=True)

        for file in files:
            src_file = Path(root) / file
            dst_file = dest_dir / file
            if not dst_file.exists() or os.path.getmtime(src_file) > os.path.getmtime(dst_file):
                shutil.copy2(src_file, dst_file)
                print(f"Updated/copied: {dst_file}")

if __name__ == "__main__":
    source = f"/afs/cern.ch/user/s/spalluot/HHbbtautau/Run3/Prod_25_09/CCLUbbtautauAnalysis/LimitsArea/inference/data/store/PlotUpperLimitsAtPoint/{model}"
    destination = "/eos/home-s/spalluot/www/HHbbtautau/Run3_nonresonant/limits_messyFolders/"
    sync_folders(source, destination)
