# polymodel development commands

set dotenv-load := false

# Canonical SQLite database URL for the projection store and the compile-time
# `query!` macros. Scoping it here (rather than via .env) keeps `just` recipes
# hermetic — see `set dotenv-load := false` above.
db-url := 'sqlite:./data/polymodel.db'

# Format Rust sources. Run clippy/check separately; clippy --fix can corrupt RSX.
fix:
    cargo fmt --all
# Run clippy without --fix; automatic clippy rewrites can corrupt Dioxus RSX.
lint:
    cargo clippy --workspace --all-targets -- -D warnings -A clippy::useless_format
    cargo clippy -p polymodel --all-targets --features server -- -D warnings -A clippy::useless_format
    cargo clippy -p polymodel --target wasm32-unknown-unknown --features web -- -D warnings -A clippy::useless_format
# Compile checks across the whole workspace plus the app's server/wasm targets.
# The server check resolves compile-time `query!` macros against the committed
# `.sqlx` offline cache; regenerate it with `just sqlx-prepare` after changing
# SQL or migrations.
check:
    cargo check --workspace
    cargo check -p polymodel --features server
    cargo check -p polymodel --target wasm32-unknown-unknown --features web

# Create and migrate the SQLite projection database.
# Required when regenerating the `.sqlx` cache, and re-run after editing migrations.
migrate:
    mkdir -p ./data
    -cargo sqlx database create --database-url '{{ db-url }}'
    cargo sqlx migrate run --source migrations --database-url '{{ db-url }}'

# Regenerate the committed `.sqlx` offline query cache after changing SQL.
# Self-contained: migrates the local database first, then points `cargo sqlx
# prepare` at it via DATABASE_URL so no manual env setup is required.
sqlx-prepare: migrate
    DATABASE_URL='{{ db-url }}' cargo sqlx prepare -- -p polymodel --features server

# Run unit tests across the workspace.
test:
    cargo nextest run --workspace

# Run server-feature tests (app only; polymodel-api has no server feature).
test-server:
    cargo nextest run -p polymodel --features server

# Run all local validation expected before review.
test-all: fix check lint test test-server
# Run browser end-to-end tests.
e2e:
    cd e2e && npm test
# Start the Dioxus dev server.
serve *ARGS:
    dx serve {{ ARGS }}
# Build the Dioxus app for web (debug).
build-web:
    dx build --platform web
# Build the Dioxus app for web (release, optimized). Strips debug symbols and runs wasm-opt.
build-web-release:
    #!/usr/bin/env bash
    set -e
    dx build --platform web --release --debug-symbols=false
    if command -v wasm-opt &>/dev/null; then
        WASM=$(find target/dx/polymodel/release/web/public -name "polymodel_bg*.wasm" -print -quit 2>/dev/null)
        if [ -n "$WASM" ]; then
            echo "==> Running wasm-opt on $WASM"
            BEFORE=$(stat -c%s "$WASM")
            wasm-opt "$WASM" -Oz --enable-bulk-memory --enable-simd --enable-nontrapping-float-to-int --enable-sign-ext -o "$WASM" || { echo "==> wasm-opt failed, keeping unoptimized wasm"; exit 0; }
            AFTER=$(stat -c%s "$WASM")
            echo "    $BEFORE -> $AFTER bytes"
        fi
    else
        echo "==> wasm-opt not found, skipping optimization"
    fi
# Regenerate crates/polymodel-api from authored lexicons via the local Jacquard checkout.
generate-api:
    nix run ../jacquard
# Create a sibling jj workspace for a Jira ticket or feature slug, then start a fresh change.
workspace-create name:
    root="$(jj workspace root)" && workspace_path="$(dirname "$root")/{{ name }}" && jj workspace add "$workspace_path" -r @
    root="$(jj workspace root)" && workspace_path="$(dirname "$root")/{{ name }}" && cd "$workspace_path" && jj desc -m "{{ name }}"

# List jj workspaces.
workspace-list:
    jj workspace list

# Forget a jj workspace after the work is safely landed or intentionally abandoned.
workspace-forget name:
    jj workspace forget {{ name }}

# Show current jj state and diff.
status:
    jj status
    jj diff --summary

# Clean build artifacts.
clean:
    cargo clean
    rm -rf target/dx
