add page to main menu (frontend)

In order to know what are locations provided by theme for menus:

$locations = get_nav_menu_locations();

This command will return an associative array that has for keys the locations and for values the ids of menu assigned to that location.

To know what pages are associated to a menu:

$idmenu = $locations['main-menu'];
$menu = wp_get_nav_menu_object( $idmenu );
$pagesItem = wp_get_nav_menu_items( $menu, ["object"=>"page"] );

In order to generate a link to a page in a menu

    $pageItem = array(
        "menu-item-object-id" => $page->ID,
        "menu-item-object" => "page",
        "menu-item-type" => "post_type",
        "menu-item-title" => $page->post_title,
        "menu-item-url" => get_permalink( $page->ID ),
        "menu-item-status" => "publish",
    );

    wp_update_nav_menu_item($idmenu, 0, $pageItem);