Adding the comment to wrong position heading

@alexey.noskov I am adding a comment to the wrong position number heading. below mentioned code is working fine but it’s overriding the correct heading or rewriting the correct heading. Kindly help me asap.
Kindly find the below mentioned screen short.

Please find the below mentioned input and expected output.
Expected_output_comment.docx (24.5 KB)
Input_comment.docx (23.1 KB)

public static void ProcessStatements(Document doc)
{
    doc.StartTrackRevisions("Hi", DateTime.Now);

    string headingToFind = "PURPOSE & SCOPE";
    int position = 2; // Position to insert the heading if not found.
    bool headingIsMissing = true;
    var list = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();
    var listHeadings = list.Where(p => p.IsListItem && p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1); // only list items with heading1 level paragraphs

    foreach (Paragraph paragraph in listHeadings)
    {
        foreach (Run run in paragraph.Runs)
        {
            paragraph.JoinRunsWithSameFormatting();

            // search for the heading
            if (run.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
            {
                int listItemPosition = 0;
                listItemPosition++;
                if (listItemPosition != position)
                {
                    var commenttextTopMarginText = "hello this is worng";

                    Comment comment = new Comment(doc, "mmm", "007", DateTime.Today);
                    comment.Paragraphs.Add(new Paragraph(doc));
                    comment.FirstParagraph.AppendChild(new Run(doc, commenttextTopMarginText));
                    CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
                    CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

                    run.ParentNode.InsertBefore(start, run);
                    run.ParentNode.InsertAfter(comment, run);
                    run.ParentNode.InsertAfter(end, run);
                }
            }
        }
    }
    doc.StopTrackRevisions();
}

@Manasahr You should avoid using paragraph.JoinRunsWithSameFormatting(), since it is also make changes to the paragraph. Please modify your code like this:

public static void ProcessStatements(Document doc)
{
    doc.StartTrackRevisions("Hi", DateTime.Now);

    string headingToFind = "PURPOSE & SCOPE";
    int position = 2; // Position to insert the heading if not found.
    bool headingIsMissing = true;
    var list = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();
    var listHeadings = list.Where(p => p.IsListItem && p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1); // only list items with heading1 level paragraphs

    foreach (Paragraph paragraph in listHeadings)
    {
        if (paragraph.ToString(SaveFormat.Text).Trim().Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
        {
            int listItemPosition = 0;
            listItemPosition++;
            if (listItemPosition != position)
            {
                var commenttextTopMarginText = "hello this is worng";

                Comment comment = new Comment(doc, "mmm", "007", DateTime.Today);
                comment.Paragraphs.Add(new Paragraph(doc));
                comment.FirstParagraph.AppendChild(new Run(doc, commenttextTopMarginText));
                CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
                CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

                paragraph.PrependChild(start);
                paragraph.AppendChild(comment);
                paragraph.AppendChild(end);
            }
        }
    }
    doc.StopTrackRevisions();
}