# Check database performance and optimization status
$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "[OK] Mounting AD2 C$ share..." -ForegroundColor Green
New-PSDrive -Name AD2 -PSProvider FileSystem -Root "\\192.168.0.6\C$" -Credential $cred -ErrorAction Stop | Out-Null

# Get server.js content to check timeout settings
Write-Host "[OK] Checking server.js configuration..." -ForegroundColor Green
$serverJs = Get-Content "AD2:\Shares\testdatadb\server.js" -Raw

if ($serverJs -match "timeout") {
    Write-Host "[FOUND] Timeout configuration in server.js" -ForegroundColor Yellow
    $serverJs -split "`n" | Where-Object { $_ -match "timeout" } | ForEach-Object {
        Write-Host "  $_" -ForegroundColor Cyan
    }
} else {
    Write-Host "[INFO] No explicit timeout configuration found" -ForegroundColor Cyan
}

# Check if better-sqlite3 is configured for performance
if ($serverJs -match "pragma") {
    Write-Host "[FOUND] SQLite PRAGMA settings:" -ForegroundColor Green
    $serverJs -split "`n" | Where-Object { $_ -match "pragma" } | ForEach-Object {
        Write-Host "  $_" -ForegroundColor Cyan
    }
} else {
    Write-Host "[WARNING] No PRAGMA performance settings found in server.js" -ForegroundColor Yellow
    Write-Host "  Consider adding: PRAGMA journal_mode = WAL, PRAGMA synchronous = NORMAL" -ForegroundColor Yellow
}

# Check routes/api.js for query optimization
Write-Host "`n[OK] Checking API routes..." -ForegroundColor Green
if (Test-Path "AD2:\Shares\testdatadb\routes\api.js") {
    $apiJs = Get-Content "AD2:\Shares\testdatadb\routes\api.js" -Raw

    # Check for LIMIT clauses
    $hasLimit = $apiJs -match "LIMIT"
    if ($hasLimit) {
        Write-Host "[OK] Found LIMIT clauses in queries (good for performance)" -ForegroundColor Green
    } else {
        Write-Host "[WARNING] No LIMIT clauses found - queries may return too many results" -ForegroundColor Yellow
    }

    # Check for index usage
    $hasIndexHints = $apiJs -match "INDEXED BY" -or $apiJs -match "USE INDEX"
    if ($hasIndexHints) {
        Write-Host "[OK] Found index hints in queries" -ForegroundColor Green
    } else {
        Write-Host "[INFO] No explicit index hints (relying on automatic optimization)" -ForegroundColor Cyan
    }
}

# Check database file fragmentation
Write-Host "`n[OK] Checking database file stats..." -ForegroundColor Green
$dbFile = Get-Item "AD2:\Shares\testdatadb\database\testdata.db"
Write-Host "  File size: $([math]::Round($dbFile.Length/1MB,2)) MB" -ForegroundColor Cyan
Write-Host "  Last accessed: $($dbFile.LastAccessTime)" -ForegroundColor Cyan
Write-Host "  Last modified: $($dbFile.LastWriteTime)" -ForegroundColor Cyan

# Suggestion to run VACUUM
$daysSinceModified = (Get-Date) - $dbFile.LastWriteTime
if ($daysSinceModified.TotalDays -gt 7) {
    Write-Host "`n[SUGGESTION] Database hasn't been modified in $([math]::Round($daysSinceModified.TotalDays,1)) days" -ForegroundColor Yellow
    Write-Host "  Consider running VACUUM to optimize database file" -ForegroundColor Yellow
}

Remove-PSDrive -Name AD2 -ErrorAction SilentlyContinue
Write-Host "`n[OK] Done" -ForegroundColor Green
