# Synopsis: Index files and assets in workspace.
# Description: Conducts recursive scans of directories to catalog available assets.

param(
    [string]$WorkspacePath = ""
)

if ([string]::IsNullOrEmpty($WorkspacePath)) {
    # Dynamically find the workspace root relative to this script
    $WorkspacePath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
}

$includeExtensions = @("*.txt", "*.ps1", "*.md", "*.json", "*.csv", "*.bat", "*.html")
$files = Get-ChildItem -Path $WorkspacePath -Include $includeExtensions -File -Recurse

$report = "=== WORKSPACE INDEX & PREVIEW ===`n"
$report += "Scan Time: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`n`n"

foreach ($file in $files) {
    if ($file.FullName -match 'chat_history\.json') { continue }

    $report += "File: $($file.Name) | Size: $($file.Length) bytes`n"
    $report += "Path: $($file.FullName.Replace($WorkspacePath, ''))`n"
    $report += "--- Preview (First 3 Lines) ---`n"
    try {
        $preview = Get-Content -Path $file.FullName -TotalCount 3 -ErrorAction Stop
        $report += ($preview -join "`n") + "`n"
    } catch {
        $report += "[Unreadable or Empty]`n"
    }
    $report += "-------------------------------`n`n"
}

Write-Output $report