from pathlib import Path


def get_mirror_path(path_from: Path, path_to: Path) -> Path:
    """Return the mirror path from a path to another one.

    The mirrored path is determined from the current working directory (cwd)
    and the path_from. The path_from shall be under the cwd. The mirrored
    relative path is the part of path_from that has no common ancestor with
    path_to, without its first parent. For example, given cwd, if path_from is
    c/d/e and path_to is /x/y, then it returns /x/y/d/e (no common ancestor
    between path_from and path_to). If path_to is c/x/y, then cwd/c/x/y/e is
    returned.

    Args:
        path_from: Path to be mirrored from.
        path_to: Root path to be mirrored to.

    Returns:
        Mirror path.

    Raises:
        ValueError: If path_from is not under the cwd.
    """
    cwd = str(Path.cwd().resolve())
    path_from = path_from.resolve(True)

    try:
        relative_from = path_from.relative_to(cwd)
    except ValueError:
        # re-raise with better message
        msg = (
            f"the current working directory {cwd} shall be a parent directory of "
            f"the inputs directory {path_from}"
        )
        raise ValueError(msg)

    # number of directory levels to skip for determining the mirrored relative
    # path
    offset = 1

    try:
        relative_to = path_to.relative_to(cwd)
    except ValueError:
        pass
    else:
        # find the common path part between from and to
        for part_from, part_to in zip(relative_from.parts, relative_to.parts):
            if part_from == part_to:
                offset += 1

    return path_to.joinpath(*relative_from.parts[offset:])