def insert_page(n, page)
            raise IndexError, "Page numbers are referenced starting from 1" if n < 1

            kids = self.Kids
            unless kids.is_a?(Array)
                raise InvalidPageTreeError, "Kids must be an Array"
            end

            count = 0
            kids.each_with_index do |kid, index|
                node = kid.solve

                case node
                when Page
                    count = count + 1
                    if count == n
                        kids.insert(index, page)
                        page.Parent = self
                        self.Count += 1
                        return self
                    end

                when PageTreeNode
                    count = count + node.Count
                    if count >= n
                        node.insert_page(n - count + node.Count, page)
                        self.Count += 1
                        return self
                    end
                else
                    raise InvalidPageTreeError, "not a Page or PageTreeNode"
                end
            end

            raise IndexError, "Out of order page index" unless count + 1 == n

            self.append_page(page)
        end
def insert_page(n, page)
            raise IndexError, "Page numbers are referenced starting from 1" if n < 1

            kids = self.Kids
            unless kids.is_a?(Array)
                raise InvalidPageTreeError, "Kids must be an Array"
            end

            count = 0
            kids.each_with_index do |kid, index|
                node = kid.solve

                case node
                when Page
                    count = count + 1
                    if count == n
                        kids.insert(index, page)
                        page.Parent = self
                        self.Count += 1
                        return self
                    end

                when PageTreeNode
                    count = count + node.Count
                    if count >= n
                        node.insert_page(n - count + node.Count, page)
                        self.Count += 1
                        return self
                    end
                else
                    raise InvalidPageTreeError, "not a Page or PageTreeNode"
                end
            end

            raise IndexError, "Out of order page index" unless count + 1 == n

            self.append_page(page)
        end
Inserts a page into the node at a specified position (starting from 1).