from pathlib import Path


def bids_output_name(phsiyo_path, output_root):
    """
    Create file name based on BIDS
    """
    phsiyo_file = Path(phsiyo_path)
    filename_root = phsiyo_file.name.split("_physio")[0]
    subject = filename_root.split("_")[0].split("sub-")[-1]
    if "ses" in filename_root:
        session = filename_root.split("_")[1].split("ses-")[-1]
        output_dir = Path(output_root) / "pyretroicor" / f"sub-{subject}" / f"ses-{session}" 
        out_name = f"{filename_root}_desc-retroicor_regressors.tsv"
    else:
        output_dir = Path(output_root) / "pyretroicor" / f"sub-{subject}"
        out_name = f"{filename_root}_desc-retroicor_regressors.tsv"

    # create output dir if not exist
    output_dir.mkdir(parents=True, exist_ok=True)
    filepath = output_dir / out_name
    return filepath