# data_dp_ssis2sp — task runner (macOS/Linux mirror of run.bat)
# Requires: just (https://github.com/casey/just) + uv.
# `just` on its own lists every recipe.

# Load .env so recipes (notably `publish`) see UV_PUBLISH_TOKEN.
# `uv run` already reads .env on its own; this extends it to `uv publish`.
set dotenv-load := true

default:
    @just --list

# ---------------------------------------------------------------------------
# environment
# ---------------------------------------------------------------------------

# editable install + all default dependency groups
install:
    uv sync

# refresh uv.lock after changing dependencies
lock:
    uv lock

# ---------------------------------------------------------------------------
# quality gates
# ---------------------------------------------------------------------------

test:
    uv run pytest

cov:
    uv run pytest --cov=data_dp_ssis2sp --cov-report=term-missing

lint:
    uv run ruff check .

fmt:
    uv run ruff format .

typecheck:
    uv run mypy data_dp_ssis2sp validation

# run every pre-commit hook (ruff, bandit, vulture) over the whole tree
hooks:
    uv run pre-commit run --all-files

# full local gate: lint -> typecheck -> test (stops at first failure)
check: lint typecheck test

# ---------------------------------------------------------------------------
# differential validation harness
# ---------------------------------------------------------------------------

validate:
    uv run pytest validation/ -m validation

validate-unit:
    uv run pytest validation/tests

# ---------------------------------------------------------------------------
# release / PyPI
# ---------------------------------------------------------------------------

# remove build artefacts so a release never ships stale files
clean:
    rm -rf dist build data_dp_ssis2sp.egg-info

# bump __version__ in data_dp_ssis2sp/__init__.py (patch|minor|major)
version level="patch":
    uvx hatch version {{level}}

# build sdist + wheel into dist/ (cleans first)
build: clean
    uv build

# smoke-test the freshly built wheel in a throwaway env
verify-build: build
    uvx --from dist/*.whl data_dp_ssis2sp --help
    # W1/GAP6 installed-layout gate check: the packaged sqlfluff.cfg must resolve
    # from site-packages — a repo-root `.sqlfluff` is absent after install, so the
    # old install-relative path made every emitted proc a phantom "Error loading
    # config" block. Convert a tiny fixture with the wheel's own code and fail if
    # the emitted _batch_warnings.log carries that block.
    rm -rf "${TMPDIR:-/tmp}/data_dp_ssis2sp-verify-build"
    uvx --from dist/*.whl data_dp_ssis2sp --business-name awb --environment-name dev convert-tree tests/fixtures/main_first "${TMPDIR:-/tmp}/data_dp_ssis2sp-verify-build" --no-publish
    ! grep -q "Error loading config" "${TMPDIR:-/tmp}/data_dp_ssis2sp-verify-build/_batch_warnings.log"

# upload to TestPyPI — needs a *TestPyPI* API token in .env as UV_PUBLISH_TOKEN.
# uv authenticates as __token__ from the env var; do NOT enter a username.
publish-test: build
    @test -n "${UV_PUBLISH_TOKEN:-}" || { echo "ERROR: UV_PUBLISH_TOKEN not set. Put a TestPyPI API token (pypi-…) in .env and run 'just publish-test' (not bare 'uv publish')."; exit 1; }
    uv publish --publish-url https://test.pypi.org/legacy/ dist/*

# upload to PyPI — needs a *PyPI* API token in .env as UV_PUBLISH_TOKEN.
# uv authenticates as __token__ from the env var; do NOT enter a username.
# NOTE: `version` runs before `build` so the bump lands in the built artefacts;
# `build` cleans dist first, so only the freshly-bumped version is published.
publish: version build
    @test -n "${UV_PUBLISH_TOKEN:-}" || { echo "ERROR: UV_PUBLISH_TOKEN not set. Put a PyPI API token (pypi-…) in .env and run 'just publish' (not bare 'uv publish')."; exit 1; }
    uv publish dist/*

# ---------------------------------------------------------------------------
# misc
# ---------------------------------------------------------------------------

opus:
    @claude --dangerously-skip-permissions "/caveman" --permission-mode auto
