# BizNode Node Updater
# =====================
# Syncs latest code from dev project to this running node.
# Does NOT touch: .env  .trial  .registration  memory/  logs/  identity/
#
# Usage:
#   cd C:\nodesrunning\biznodemainnode
#   .\update_node.ps1
#
# Optional — restart containers after update:
#   .\update_node.ps1 -Restart

param(
    [switch]$Restart
)

$DEV  = "C:\Users\shashikanth ramamurt\projects\1bzbiznodev2\biznode_fixed"
$NODE = $PSScriptRoot

# Directories to sync (code only — no data folders)
$DIRS = @(
    "agent", "automation", "core", "docker",
    "graphs", "scripts", "services", "tools", "ui"
)

# Individual files to sync
$FILES = @(
    @{ src = "launcher.py.bak";                    dst = "launcher.py" },
    @{ src = "docker-compose.yml";                 dst = "docker-compose.yml" },
    @{ src = "run_dashboard.py";                   dst = "run_dashboard.py" },
    @{ src = "requirements.txt";                   dst = "requirements.txt" },
    @{ src = "status.py";                          dst = "status.py" },
    # memory code files (NOT biznode.db — that's user data)
    @{ src = "memory/__init__.py";                 dst = "memory/__init__.py" },
    @{ src = "memory/database.py";                 dst = "memory/database.py" },
    @{ src = "memory/db_adapter.py";               dst = "memory/db_adapter.py" },
    @{ src = "memory/qdrant_client.py";            dst = "memory/qdrant_client.py" },
    @{ src = "memory/execution_memory.py";         dst = "memory/execution_memory.py" },
    @{ src = "memory/obsidian_layer.py";           dst = "memory/obsidian_layer.py" }
)

# Files NEVER overwritten (user data / secrets)
$PROTECTED = @(".env", ".trial", ".registration", "identity", "memory", "logs", "backups")

Write-Host ""
Write-Host "============================================"
Write-Host "  BizNode Node Updater"
Write-Host "  From : $DEV"
Write-Host "  To   : $NODE"
Write-Host "============================================"
Write-Host ""

$updated = 0
$skipped = 0

# ── Sync directories ───────────────────────────────────────
foreach ($dir in $DIRS) {
    $src = "$DEV\$dir"
    $dst = "$NODE\$dir"

    if (-not (Test-Path $src)) {
        Write-Host "  [SKIP] $dir/ — not found in dev"
        $skipped++
        continue
    }

    # robocopy: /MIR mirrors, /XD excludes dirs, /XF excludes files, /NFL no file list, /NDL no dir list
    $result = robocopy $src $dst /MIR /XD __pycache__ /XF "*.pyc" "*.pyo" "*.bak" /NFL /NDL /NJH /NJS /nc /ns 2>&1
    Write-Host "  [OK]   $dir/"
    $updated++
}

Write-Host ""

# ── Sync individual files ──────────────────────────────────
foreach ($f in $FILES) {
    $src = "$DEV\$($f.src)"
    $dst = "$NODE\$($f.dst)"

    if (-not (Test-Path $src)) {
        Write-Host "  [SKIP] $($f.dst) — source not found"
        $skipped++
        continue
    }

    Copy-Item -Path $src -Destination $dst -Force
    $label = if ($f.src -ne $f.dst) { "$($f.src) -> $($f.dst)" } else { $f.dst }
    Write-Host "  [OK]   $label"
    $updated++
}

Write-Host ""
Write-Host "============================================"
Write-Host "  Updated : $updated item(s)"
if ($skipped -gt 0) { Write-Host "  Skipped : $skipped item(s)" }
Write-Host "  Protected (untouched): .env  .trial  .registration  memory/  logs/"
Write-Host "============================================"
Write-Host ""

# ── Optional Docker restart ────────────────────────────────
if ($Restart) {
    Write-Host "[*] Restarting Docker services..."
    & docker compose -f "$NODE\docker-compose.yml" restart bot dashboard 2>&1
    Write-Host "[OK] Services restarted."
    Write-Host ""
}

Write-Host "Done. Run 'python status.py' to verify node state."
Write-Host ""
