# Load framework/.env (gitignored), e.g. FABRICKS_REMOTE for test-apache-remote
set dotenv-load

# Point git at the repo's tracked hooks (pre-commit: format+lint)
setup-hooks:
    git -C .. config core.hooksPath .githooks

# Run linter on PATHS, default the whole tree (CI); the pre-commit hook passes the staged files
lint *PATHS:
    #!/usr/bin/env bash
    set -euo pipefail
    paths=({{ if PATHS == "" { "fabricks/ tests/" } else { PATHS } }})
    py=$(printf '%s\n' "${paths[@]}" | grep -E '(\.py|/)$' || true)
    [ -z "$py" ] || uv run ruff check --force-exclude $py
    [ -z "$py" ] || uv run ty check --force-exclude $py

# Fail on comment blocks longer than 3 lines in FILES (the pre-commit hook passes the staged files)
lint-comments *FILES:
    uv run python scripts/check_comment_size.py {{FILES}}

# Format PATHS, default the whole tree; the pre-commit hook passes the staged files (each tool only gets its own types)
format *PATHS:
    #!/usr/bin/env bash
    set -euo pipefail
    paths=({{ if PATHS == "" { "fabricks/ tests/" } else { PATHS } }})
    pick() { printf '%s\n' "${paths[@]}" | grep -E "$1" || true; }
    py=$(pick '(\.py|/)$'); sql=$(pick '(\.jinja|\.sql|/)$'); yml=$(pick '(\.ya?ml|/)$')
    [ -z "$py" ] || uv run ruff format --force-exclude $py
    [ -z "$sql" ] || uv run sqlfmt $sql
    [ -z "$yml" ] || uv run yamlfix $yml

# Install the plugins listed in skills.json that aren't in the local plugin cache yet.
install-skills:
    uv run python scripts/install_skills.py

# Refresh marketplaces, then re-sync .claude/skills/ from the plugin cache (per skills.json).
update-skills:
    uv run python scripts/update_skills.py

# Regenerate job-schema*.json (repo root) from the Pydantic job config models (fabricks/models/job_schema.py)
generate-schema:
    uv run python scripts/generate_job_schema.py

# Every test recipe tees its output to framework/.logs/<category>/<timestamp>.log (gitignored; latest.log links to the newest; FABRICKS_TEST_LOG=off disables)

# Run plain unit tests (tests/unit/plain/) -- no Spark/fabricks.context dependency
# TARGET can narrow to a file or a single test, e.g. `just test-plain tests/unit/plain/test_x.py::test_y`
test-plain TARGET="tests/unit/plain":
    scripts/run_logged.sh plain uv run pytest {{TARGET}}

# Run config-resolution unit tests (tests/unit/config/) -- real fabricks.context, Spark faked out
# TARGET can narrow to a file or a single test, e.g. `just test-config tests/unit/config/test_x.py::test_y`
test-config TARGET="tests/unit/config":
    scripts/run_logged.sh config uv run pytest {{TARGET}}

# List tests that cover exactly the same lines as another test: pruning candidates, not proof (see testing-fabricks section 9)
# TARGET is one tier, e.g. `just test-overlap tests/spark/apache` (needs Java 17-21, takes minutes); pytest-cov is fetched on the fly
test-overlap TARGET="tests/unit/config":
    mkdir -p .logs
    rm -f .logs/.coverage-overlap
    COVERAGE_FILE=.logs/.coverage-overlap uv run --with pytest-cov pytest {{TARGET}} --cov=fabricks --cov-context=test --cov-report= -q
    uv run --with pytest-cov python scripts/test_overlap.py .logs/.coverage-overlap

# Run both unit tiers: plain, then config (separate pytest processes; the tiers cannot share one), stopping on the first failure
test-unit: test-plain test-config

# Run the Apache Spark integration tests (real Spark+Delta, needs Java 17+)
# TARGET can narrow to a file or a single test, e.g. `just test-apache tests/spark/apache/test_x.py::test_y`
# WORKERS runs that many pytest-xdist workers (each is a Spark JVM), e.g. `just test-apache tests/spark/apache 4`; empty = serial
test-apache TARGET="tests/spark/apache" WORKERS="":
    # a killed run can leave a worker's Derby metastore half-created, which
    # fails to reopen (ERROR XBM0A) on the next run
    rm -rf tests/spark/apache/.worker_cwd
    scripts/run_logged.sh apache uv run pytest {{TARGET}} {{ if WORKERS == "" { "" } else { "-n " + WORKERS } }} -v --durations=20

# Run the Apache tier on a remote machine over ssh (rsyncs the working tree first); logged locally
# Needs FABRICKS_REMOTE=<ssh host>; FABRICKS_REMOTE_DIR defaults to ~/Fabricks; FABRICKS_REMOTE_JAVA_HOME optionally picks the remote JDK. Remote needs Java 17-21, uv, just.
# TARGET and WORKERS as in test-apache; WORKERS defaults to 4 here (set it to "" for a serial run)
test-apache-remote TARGET="tests/spark/apache" WORKERS="4":
    scripts/run_logged.sh apache-remote scripts/test_apache_remote.sh {{TARGET}} {{WORKERS}}

# Deploy the bundle to the 'test' workspace
deploy:
    databricks bundle deploy -t test

# Deploy the bundle and run the Databricks test suite (tests/spark/databricks/) against the 'test' workspace
test-databricks: deploy
    scripts/run_logged.sh databricks databricks bundle run fabricks_test_job -t test

# Run every test tier: plain, config, apache, then databricks (deploy + run against the 'test' workspace)
test-all: test-unit test-apache test-databricks
