set shell := ["pwsh.exe", "-NoProfile", "-c"]

default:
    @just --list

# Build and deploy everything across Windows and WSL
all: build-all deploy-all

# Build everything across Windows and WSL
build-all: build-win build-wsl
    @Write-Host "Build complete across both OS boundaries." -ForegroundColor Green

# Deploy everything across Windows and WSL
deploy-all: deploy-win deploy-wsl
    @Write-Host "Deployment complete across both OS boundaries." -ForegroundColor Green

# Gracefully shut down the running server
shutdown-server:
    @Write-Host "Shutting down MCP server gracefully..." -ForegroundColor Cyan
    -$token = Get-Content "$env:USERPROFILE\.gemini\mcp_memory\admin.token" -ErrorAction SilentlyContinue; if ($token) { Invoke-RestMethod -Uri "http://127.0.0.1:3000/shutdown" -Method Post -Headers @{ Authorization = "Bearer $token" } -ErrorAction SilentlyContinue } else { Invoke-RestMethod -Uri "http://127.0.0.1:3000/shutdown" -Method Post -ErrorAction SilentlyContinue }
    Start-Sleep -Seconds 1
    -if (Get-Process mcp-memory-server, mcp-memory-stub, mcp-memory-nvim -ErrorAction SilentlyContinue) { Stop-Process -Name mcp-memory-server, mcp-memory-stub, mcp-memory-nvim -Force -ErrorAction SilentlyContinue }
    Start-Sleep -Seconds 2

# Build Windows-native binaries
build-win: build-server build-stub-win build-nvim-win

# Deploy Windows-native binaries
deploy-win: deploy-server deploy-stub-win deploy-nvim-win deploy-rules-win

# =========================================================
# 1. LIFECYCLE (Start / Stop / Restart)
# =========================================================

# Stop the background server
stop port="3000":
    @Write-Host "Shutting down MCP server gracefully on port {{port}}..." -ForegroundColor Cyan
    -$token = Get-Content "$env:USERPROFILE\.gemini\mcp_memory\admin.token" -ErrorAction SilentlyContinue; if ($token) { Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -Headers @{ Authorization = "Bearer $token" } -ErrorAction SilentlyContinue } else { Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -ErrorAction SilentlyContinue }
    $stopped = $false; for ($i = 1; $i -le 10; $i++) { if (-not (Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue)) { $stopped = $true; Write-Host '✅ Server on port {{port}} shut down gracefully.' -ForegroundColor Green; break }; Start-Sleep -Seconds 1 }
    if (-not $stopped -and (Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue)) { Write-Host '⚠️ Server did not exit gracefully within 10s. Force killing process...' -ForegroundColor Yellow; Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue }; Start-Sleep -Seconds 2 }
    for ($i = 1; $i -le 5; $i++) { if (-not (Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue)) { break }; Start-Sleep -Seconds 1 }
    Start-Sleep -Seconds 2
    Write-Host '✅ Shutdown complete. Port {{port}} is free.' -ForegroundColor Green

# Start the server in the background (nohup equivalent)
start port="3000":
    @if (Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue) { Write-Host '⚠️ Server is already running on port {{port}}. Skipping start.' -ForegroundColor Yellow; exit 0 }; Write-Host 'Starting MCP server in background on port {{port}}...' -ForegroundColor Cyan; $env:MCP_PORT='{{port}}'; $env:MCP_UDP_PORT1=$([int]{{port}} + 1); $env:MCP_UDP_PORT2=$([int]{{port}} + 2); .\start_server.ps1 -Port {{port}}; Write-Host 'Waiting up to 40 seconds for /ping endpoint...' -ForegroundColor Cyan; for ($i = 1; $i -le 40; $i++) { try { $res = Invoke-RestMethod -Uri 'http://127.0.0.1:{{port}}/ping' -ErrorAction Stop; Write-Host '✅ Server is UP!' -ForegroundColor Green; exit 0 } catch { Start-Sleep -Seconds 1 } }; Write-Error '❌ Server failed to start on port {{port}} within 40 seconds.'; exit 1

# Verify the deployment is healthy and serving correct content
verify port="3000":
    .\verify_deployment.ps1 -Port {{port}}

# Restart the server and verify
restart port="3000": (stop port) (start port) (verify port)

# =========================================================
# 2. BUILD (Compile Binaries)
# =========================================================

build-server:
    @Write-Host "Building Windows Server..." -ForegroundColor Cyan
    cargo build --release -p mcp-memory-server

build-stub-win:
    @Write-Host "Building Windows Stub..." -ForegroundColor Cyan
    cargo build --release -p mcp-memory-stub


# =========================================================
# 3. DEPLOY (Move Binaries to Global Path)
# =========================================================
deploy-server:
    @Write-Host "Deploying Windows Server..." -ForegroundColor Cyan
    -if (Test-Path "C:\Users\reazul.ashraf\.local\bin\mcp-memory-server.exe") { Move-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-server.exe" "C:\Users\reazul.ashraf\.local\bin\mcp-memory-server.exe.old" -ErrorAction SilentlyContinue; Remove-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-server.exe.old" -ErrorAction SilentlyContinue }
    Copy-Item -Force target\release\mcp-memory-server.exe "C:\Users\reazul.ashraf\.local\bin\"


deploy-stub-win:
    @Write-Host "Deploying Windows Stub..." -ForegroundColor Cyan
    -Stop-Process -Name mcp-memory-stub -Force -ErrorAction SilentlyContinue
    -if (Test-Path "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe") { Move-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe" "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe.old" -ErrorAction SilentlyContinue; Remove-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe.old" -ErrorAction SilentlyContinue }
    Copy-Item -Force target\release\mcp-memory-stub.exe "C:\Users\reazul.ashraf\.local\bin\"

deploy-nvim-win:
    @Write-Host "Deploying Windows Nvim..." -ForegroundColor Cyan
    -Stop-Process -Name mcp-memory-nvim -Force -ErrorAction SilentlyContinue
    -if (Test-Path "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe") { Move-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe" "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe.old" -ErrorAction SilentlyContinue; Remove-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe.old" -ErrorAction SilentlyContinue }
    Copy-Item -Force target\release\mcp-memory-nvim.exe "C:\Users\reazul.ashraf\.local\bin\"


deploy-rules-win:
    @Write-Host "Deploying global Agent rules (Windows)..." -ForegroundColor Cyan
    if (!(Test-Path "C:\Users\reazul.ashraf\.gemini\config\rules")) { New-Item -ItemType Directory -Force -Path "C:\Users\reazul.ashraf\.gemini\config\rules" | Out-Null }
    Copy-Item -Force -Recurse agent-rules\* "C:\Users\reazul.ashraf\.gemini\config\rules\"

# Build WSL-native binaries
build-wsl: build-stub build-nvim

# Deploy WSL-native binaries
deploy-wsl: deploy-stub deploy-nvim deploy-rules-wsl

build-stub:
    @Write-Host "Building WSL Stub natively..." -ForegroundColor Cyan
    wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cargo build --release -p mcp-memory-stub'

deploy-stub:
    @Write-Host "Deploying WSL Stub natively..." -ForegroundColor Cyan
    wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cp target/release/mcp-memory-stub /home/riz/.local/bin/'

build-nvim:
    @Write-Host "Building WSL Nvim natively..." -ForegroundColor Cyan
    wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cargo build --release -p mcp-memory-nvim'




deploy-nvim:
    @Write-Host "Deploying WSL Nvim natively..." -ForegroundColor Cyan
    wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cp target/release/mcp-memory-nvim /home/riz/.local/bin/'

deploy-rules-wsl:
    @Write-Host "Deploying global Agent rules (WSL)..." -ForegroundColor Cyan
    wsl.exe -d Ubuntu -e bash -c 'mkdir -p /home/riz/.gemini/config/rules && cp -r /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory/agent-rules/* /home/riz/.gemini/config/rules/'

# Fast parallel test execution using cargo-nextest (default test command)
test:
    @Write-Host "Running fast parallel tests via cargo-nextest..." -ForegroundColor Cyan
    cargo nextest run --workspace

# Standard sequential cargo test execution
test-cargo:
    @Write-Host "Running standard unit tests across workspace..." -ForegroundColor Cyan
    cargo test --workspace

# Run configuration tests to ensure eagerTools parity
test-config:
    cargo test --release -p mcp-memory-server --test parity_test

# Run tests and generate code coverage report with matching rustup LLVM toolchain
test-coverage:
    @Write-Host "Running tests with code coverage..." -ForegroundColor Cyan
    cargo llvm-cov --workspace --ignore-filename-regex "stub" -- --test-threads=1




# Start the server directly
startup-server:
    cargo run -p mcp-memory-server

# --- Explicit Lifecycle Chaining Commands ---
# all-* commands handle the FULL end-to-end process: Stop -> Build -> Deploy -> Start
all-server-win port="3000": (stop port) build-server deploy-server (start port) (verify port)
all-stub-win: build-stub-win deploy-stub-win
all-nvim-win: build-nvim-win deploy-nvim-win
all-stub-wsl: build-stub deploy-stub
all-nvim-wsl: build-nvim deploy-nvim
all-server-wsl port="3000": (stop port) build-server deploy-server (start port) (verify port)




build-nvim-win:
    @Write-Host 'Building Windows Nvim...' -ForegroundColor Cyan
    cargo build --release -p mcp-memory-nvim

# Auto-generate & verify unit tests using local NAS Ollama ($0 tokens)
improve-tests file="server/src/ollama.rs":
    improve-test {{file}}

