# 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
    cargo clippy -p polymodel --all-targets --features server -- -D warnings
    cargo clippy -p polymodel --target wasm32-unknown-unknown --features web -- -D warnings
# 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:
    dx serve
# Build the Dioxus app for web.
build-web:
    dx build --platform web
# 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
