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-win-nvim -ErrorAction SilentlyContinue) { Stop-Process -Name mcp-memory-server, mcp-memory-stub, mcp-memory-win-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 }
    Start-Sleep -Seconds 1
    -Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue }
    Start-Sleep -Seconds 2

# 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)
# =========================================================
# WARNING: Deploy recipes DO NOT stop running processes. 
# You MUST stop the server/process before deploying to avoid file lock errors.

deploy-server:
    @Write-Host "Deploying Windows Server..." -ForegroundColor Cyan
    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
    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-win-nvim -Force -ErrorAction SilentlyContinue
    Copy-Item -Force target\release\mcp-memory-win-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="$PATH:/home/riz/.cargo/bin" && 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="$PATH:/home/riz/.cargo/bin" && 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="$PATH:/home/riz/.cargo/bin" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cargo build --release -p mcp-memory-linux-nvim'

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

# 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
test-coverage:
    @Write-Host "Running tests with code coverage..." -ForegroundColor Cyan
    rustup run stable cargo llvm-cov --workspace

# 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-win-nvim
