Site icon Hip-Hop Website Design and Development

How can I show links for custom posts group by taxonomy current term and sub-term?

I tried to display post with links by sub term and group thouse posts and sub terms by parent terms.
For example:

Parent term 1

Instead of this I receive:

Here is my current code:

$all_terms = get_terms(['taxonomy' => 'course-category', 'hide_empty' => 1 ] );
if (!$all_terms){
    $Content = '<h2>no terms</h2>';;
    
}
foreach (  $all_terms as $term ) { // external
    $query = new WP_Query( [
        'post_status' => 'publish',
        'post_type' => 'course'
        
        
    ]);
    if ( ! $query->have_posts() ) continue;
    $Content .= '<h3>' . $term->name . '</h3>';
    $Content .= "<div>";
    while ( $query->have_posts() ) {// internal
        $query->the_post();
        $Content .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
        
       
    }
    $Content .= "</div>";

I don’t realy understand how to use get_terms(), and maybe, get_term_children() for deal with this.

Help me please to solve this …
I’ll be grateful for any advice you can share.
Thanks.