<#
We will need to add <xml> at the start of the document and </xml> at the end
#>

Function CreateWikijsPage {
	param ( $InputPagePath , $TitleOfTheArticle)

$GraphQLEndpoint = "https://wikijs.tryton.geeraert.eu/graphql"

$AccessToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGkiOjIsImdycCI6MSwiaWF0IjoxNzY4OTkzNzA4LCJleHAiOjE4MDA1NTEzMDgsImF1ZCI6InVybjp3aWtpLmpzIiwiaXNzIjoidXJuOndpa2kuanMifQ.ivUT8LUM-vul3JykAUPp4dnldxat8-kUvrsjQ51tuoE6dXJLgsZm-FYzGVQBRRMpZXbWIDjy0wuPJwEaZxMywN2O2ys5UrmIO5MxCydV8zeiPooPHKOhtU6rbAPGdICHRjpneiIx5dmahzGN2FDzqhIeR4_H2pioYWLih1ohlmDmUGsTpcJgSY73O0_xDa_gnVwAdsmwDRqwZSdd04ZCCYpzBIkR9YR1kPtxtX9UHonWRAYvKJckCQYUjTiHUWU1pBKnHWsxsygMqgj56rRufs4C37i_WGKBKw-NXjRiS6Z32oTBDOL9Rv_YMhmhwHLb3Hb_eEtl2nXYPelEXxGWUw"

# Define the new page details
#$NewPagePath = "/Work/Deployments/Tutorial/my_new_article"
$NewPagePath = $InputPagePath
$PageTitle = "My New Article"
$PageContent = @'
# This is a new article

Created via PowerShell and GraphQL.
'@

$Headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "Bearer $AccessToken"
}

$GraphQLMutation = @'
mutation CreatePage {
  pages {
    create(
      content: "This is the content of the new page created via PowerShell.",
      description: "",
      editor: "markdown",
      isPublished: true,
      isPrivate: false,
      locale: "en",
      path: "InputPagePath",
      tags: [],
      title: "titleofthearticle"
    ) {
      responseResult {
        succeeded
        errorCode
        slug
        message
      }
      page {
        id
        path
        title
      }
    }
  }
}
'@


$GraphQLMutation = $GraphQLMutation.Replace("InputPagePath", $InputPagePath)
$GraphQLMutation = $GraphQLMutation.Replace("titleofthearticle", $TitleOfTheArticle)


$Body = @{
    query = $GraphQLMutation
} | ConvertTo-Json -Depth 3

try {
    $response = Invoke-RestMethod -Uri $GraphQLEndpoint -Method Post -Headers $Headers -Body $Body
    $result = $response.data.pages.create
    if ($result.responseResult.succeeded) {
        Write-Host "✅ Page created successfully!"
        $result.page | Format-Table id, title, path
    } else {
        Write-Error "❌ Failed: $($result.responseResult.message)"
    }
} catch {
    Write-Error "GraphQL request failed: $_"
}

}

[xml]$cn = Get-Content "C:\Users\pgeeraert\Downloads\export-onenote\Deployments-xml.xml"
#$cn.xml.Section.Page.Name

$illegal = [string]::join(' ',"'",'+','🛠️',([System.IO.Path]::GetInvalidFileNameChars()))

foreach ($Section in $cn.xml.Section) {
    $section_name = $Section.name
    #write-output "Section: $section_name"
    $section_name_replaced = $section_name -replace "[$illegal]",'_'
    #write-output "Section replaced: $section_name_replaced"
 
    foreach ($Page in $Section.Page) {
    $page_name = $Page.name
    #write-output "Page: $page_name"
    $page_name_replaced = $page_name -replace "[$illegal]",'_'
    #write-output "Page replaced: $page_name_replaced"
 
    #/Work/Deployments/Tutorial/another-testing"
    $path = "/Deployments/$section_name_replaced/$page_name_replaced"
    write-output $path
    Write-Output $page_name
    CreateWikijsPage -InputPagePath $path -TitleOfTheArticle $page_name
    #sleep 5
    }
}






