# PowerShell script — DBeaver Connecties Automatisch Toevoegen
# ============================================================
# Dit script detecteert DBeaver en voegt PostgreSQL + Neo4j connecties toe.
#
# Gebruik:
#   powershell -ExecutionPolicy Bypass -File setup_dbeaver.ps1

$ErrorActionPreference = "Stop"

Write-Host "=== DBeaver Connectie Setup ===" -ForegroundColor Cyan
Write-Host ""

# 1. Zoek DBeaver installatie
$dbeaverPaths = @(
    "$env:APPDATA\DBeaverData",
    "$env:LOCALAPPDATA\DBeaver",
    "$env:USERPROFILE\AppData\Roaming\DBeaverData",
    "$env:USERPROFILE\.dbeaver"
)

$found = $false
foreach ($path in $dbeaverPaths) {
    if (Test-Path $path) {
        Write-Host "DBeaver data gevonden op: $path" -ForegroundColor Green
        $found = $true
        
        # Zoek alle workspace directories
        $workspaces = Get-ChildItem -Path $path -Directory -Filter "workspace*" -ErrorAction SilentlyContinue
        
        foreach ($ws in $workspaces) {
            $dbeaverDir = Join-Path $ws.FullName "General\.dbeaver"
            if (-not (Test-Path $dbeaverDir)) {
                New-Item -ItemType Directory -Path $dbeaverDir -Force | Out-Null
            }
            
            $configFile = Join-Path $dbeaverDir "data-sources.json"
            
            Write-Host "  Workspace: $($ws.Name) -> $configFile" -ForegroundColor Gray
            
            # Lees bestaande config (of maak nieuwe)
            $config = $null
            if (Test-Path $configFile) {
                try {
                    $config = Get-Content $configFile -Raw | ConvertFrom-Json
                    Write-Host "    Bestaande config gevonden: $(($config.connections.PSObject.Properties | Measure-Object).Count) connecties" -ForegroundColor Gray
                } catch {
                    Write-Host "    Waarschuwing: Kon bestaande config niet lezen, maak nieuwe" -ForegroundColor Yellow
                }
            }
            
            if (-not $config -or -not $config.connections) {
                $config = [PSCustomObject]@{
                    folders = @{}
                    connections = @{}
                }
            }
            
            # Voeg PostgreSQL connectie toe (als die nog niet bestaat)
            $pgConnId = "postgresql-homelab-dashboard"
            if (-not $config.connections.$pgConnId) {
                $config.connections | Add-Member -MemberType NoteProperty -Name $pgConnId -Value ([PSCustomObject]@{
                    provider = "postgresql"
                    driver = "postgresql-jdbc"
                    name = "Homelab PostgreSQL (Dashboard)"
                    host = "192.168.1.211"
                    port = "5433"
                    database = "homelab"
                    user = "mo"
                    password = "WaQTUw2t"
                    savePassword = $true
                    configurationType = "MANUAL"
                    showSystemObjects = $false
                    properties = @{
                        connectTimeout = "20"
                        loginTimeout = "20"
                    }
                })
                Write-Host "    + PostgreSQL connectie toegevoegd" -ForegroundColor Green
            } else {
                Write-Host "    PostgreSQL connectie bestaat al" -ForegroundColor Gray
            }
            
            # Voeg Neo4j connectie toe
            $neoConnId = "neo4j-homelab-network"
            if (-not $config.connections.$neoConnId) {
                $config.connections | Add-Member -MemberType NoteProperty -Name $neoConnId -Value ([PSCustomObject]@{
                    provider = "neo4j"
                    driver = "neo4j-jdbc"
                    name = "Homelab Neo4j (Netwerk)"
                    host = "192.168.1.211"
                    port = "49153"
                    url = "neo4j://192.168.1.211:49153"
                    user = "neo4j"
                    password = "WaQTUw2t"
                    savePassword = $true
                    configurationType = "MANUAL"
                })
                Write-Host "    + Neo4j connectie toegevoegd" -ForegroundColor Green
            } else {
                Write-Host "    Neo4j connectie bestaat al" -ForegroundColor Gray
            }
            
            # Schrijf config terug
            $config | ConvertTo-Json -Depth 5 | Set-Content $configFile -Encoding UTF8
            Write-Host "    Config opgeslagen!" -ForegroundColor Green
        }
        break
    }
}

if (-not $found) {
    Write-Host "DBeaver NIET gevonden op dit systeem!" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Handmatig DBeaver connecties toevoegen:" -ForegroundColor White
    Write-Host ""
    Write-Host "1. Open DBeaver"
    Write-Host "2. Database → New Database Connection"
    Write-Host ""
    Write-Host "PostgreSQL:"
    Write-Host "   Host: 192.168.1.211  Port: 5433"
    Write-Host "   Database: homelab  User: mo"
    Write-Host ""
    Write-Host "Neo4j:"
    Write-Host "   URI: neo4j://192.168.1.211:49153"
    Write-Host "   User: neo4j"
    Write-Host ""
    
    # Maak een import-bestand voor later gebruik
    $importConfig = @"
{
  "folders": {},
  "connections": {
    "postgresql-homelab": {
      "provider": "postgresql",
      "driver": "postgresql-jdbc",
      "name": "Homelab PostgreSQL",
      "host": "192.168.1.211",
      "port": "5433",
      "database": "homelab",
      "user": "mo",
      "savePassword": true,
      "configurationType": "MANUAL"
    },
    "neo4j-homelab": {
      "provider": "neo4j",
      "driver": "neo4j-jdbc",
      "name": "Homelab Neo4j",
      "url": "neo4j://192.168.1.211:49153",
      "user": "neo4j",
      "savePassword": true,
      "configurationType": "MANUAL"
    }
  }
}
"@
    $importFile = Join-Path $PSScriptRoot "dbeaver-connections-import.json"
    $importConfig | Set-Content $importFile -Encoding UTF8
    Write-Host "Import-bestand gemaakt: $importFile" -ForegroundColor Green
    Write-Host ""
    Write-Host "In DBeaver: File → Import → DBeaver → Connections"
    Write-Host "Selecteer: $importFile"
}

Write-Host ""
Write-Host "Let op: herstart DBeaver om de connecties te zien!" -ForegroundColor Cyan
