# Locate-WorkspaceRoot.ps1
# Robustly locates the workspace root and identifies files using proper parenthetical expressions

Write-Output "--- LOCATE WORKSPACE ROOT ---"
$SearchFile = "Scripts\AgentOSH_Chat.html"

# Safely get MyDocuments folder path
$MyDocs = [System.Environment]::GetFolderPath("MyDocuments")
Write-Output "My Documents Path: $MyDocs"

# Define paths to test
$SearchPaths = @(
    $pwd.Path,
    (Join-Path $MyDocs "AgentOS"),
    (Join-Path $MyDocs "MirrorOS"),
    (Join-Path $MyDocs "AgentOS"),
    (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent)
)

foreach ($Path in $SearchPaths) {
    if (-not $Path) { continue }
    $TestFile = Join-Path $Path $SearchFile
    Write-Output "Testing path: $TestFile"
    if (Test-Path $TestFile) {
        Write-Output ">>> FOUND ROOT AT: $Path"
        $TargetExcel = Join-Path $Path "Deliverables\AgentOS_Workspace_Manifest.xlsx"
        if (Test-Path $TargetExcel) {
            Write-Output "Excel exists here: $TargetExcel ($((Get-Item $TargetExcel).Length) bytes)"
        } else {
            Write-Output "Excel does NOT exist here: $TargetExcel"
        }
    }
}
Write-Output "------------------------------"
