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

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

deploy-server: shutdown-server
    Write-Host "Deploying Windows Server..." -ForegroundColor Cyan
    Copy-Item -Force target\release\mcp-memory-server.exe "C:\Users\reazul.ashraf\.local\bin\"

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

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\"

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

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-wsl build-nvim-wsl

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

build-stub-wsl:
    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-wsl:
    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-wsl:
    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-wsl:
    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

# Stop the background server
stop: shutdown-server

# Start the server in the background (nohup equivalent)
start:
    Write-Host "Starting MCP server in background..." -ForegroundColor Cyan
    Start-Process -WindowStyle Hidden -FilePath mcp-memory-server

# Verify the deployment is healthy and serving correct content
verify:
    pwsh.exe -NoProfile -File verify_deployment.ps1

# Restart the server and verify
restart: stop start verify

