# Check for error logs on AD2
$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

Write-Host "[OK] Checking for WAL files..." -ForegroundColor Green
$dbFolder = "AD2:\Shares\testdatadb\database"
$walFiles = Get-ChildItem $dbFolder -Filter "*.wal" -ErrorAction SilentlyContinue
$shmFiles = Get-ChildItem $dbFolder -Filter "*.shm" -ErrorAction SilentlyContinue

if ($walFiles -or $shmFiles) {
    Write-Host "[FOUND] WAL files exist:" -ForegroundColor Green
    $walFiles | ForEach-Object { Write-Host "  $_" -ForegroundColor Cyan }
    $shmFiles | ForEach-Object { Write-Host "  $_" -ForegroundColor Cyan }
} else {
    Write-Host "[INFO] No WAL files found" -ForegroundColor Yellow
}

Write-Host "`n[OK] Checking deployed api.js..." -ForegroundColor Green
$apiContent = Get-Content "AD2:\Shares\testdatadb\routes\api.js" -Raw
if ($apiContent -match "readonly: true" -and $apiContent -match "journal_mode = WAL") {
    Write-Host "[ERROR] CONFLICT DETECTED!" -ForegroundColor Red
    Write-Host "  Cannot set WAL mode on readonly database!" -ForegroundColor Red
    Write-Host "  This is causing the database query errors" -ForegroundColor Red
}

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