"""
Sulci Reconstruction
====================

Simple example.

Example on how to run the sulci reconstruction and identification
pre-processing using BrainPrep.
See :ref:`user guide <sulcirec>` for details.

Data
----

Let's first get some anatomical data.
"""

from pathlib import Path
from brainprep.datasets import IBCDataset

datadir = Path("/tmp/brainprep-data")
datadir.mkdir(parents=True, exist_ok=True)
dataset = IBCDataset(datadir)
data = dataset.fetch(
    subject="01",
    modality="func",
)
print(data)


# %%
# Analysis
# --------
# 
# Let's now perform the preprocessing using BrainPrep.
# As with many tutorials, we won't execute the code directly here.
# However, feel free to set the 'dryrun' configuration to False
# to actually run each step and generate results on disk.

import shutil
from brainprep.workflow import (
    brainprep_group_sulcirec,
    brainprep_sulcirec,
)
from brainprep.config import Config
from brainprep.reporting import RSTReport

outdir = Path("/tmp/brainprep-sulcirec")
if outdir.is_dir():
    shutil.rmtree(outdir)
outdir.mkdir(parents=True, exist_ok=True)
with Config(dryrun=True, verbose=True):
    report = RSTReport()
    brainprep_sulcirec(
        t1_file=data.anat,
        output_dir=outdir,
        keep_intermediate=False,
    )
    print(report)
    brainprep_group_sulcirec(
        output_dir=outdir,
        keep_intermediate=False,
    )


# %%
# CLI
# ---
# 
# Let's now generate the same analysis using the CLI. The goal here is to
# translate the workflow calls into explicit shell commands.
# See :ref:`user guide <cli>` for details.

from pprint import pprint

commands = []
commands.append(
    [
        [
            "brainprep", "subject-level-sulcirec",
            "--t1-file", str(data.anat),
            "--output-dir", str(outdir),
        ]
    ]
)
commands.append(
    [
        [
            "brainprep", "group-level-sulcirec",
            "--output-dir", str(outdir),
        ],
    ]
)
pprint(commands)


# %%
# Container
# ---------
# 
# Note that the commands generated by the CLI are not limited to being
# displayed for reference; they can also be executed directly within the
# workflow‑dedicated container. By running the commands inside the container,
# you benefit from a controlled runtime context where all necessary
# dependencies, libraries, and configuration files are already available.
# In practice, this means that once the CLI has produced the appropriate
# instructions, you can simply copy and run them inside the container to
# achieve the intended results. You can find the BrainPrep images on Docker
# Hub: `Neurospin Docker Hub <https://hub.docker.com/u/neurospin>`_.
