# 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

# table-parity suite (TP3): checksum-compare each _tables.json manifest table
# between the SOURCE and TARGET servers; skips cleanly when no server reachable
parity:
    uv run pytest data_dp_ssis2sp/parity_suite -m parity

# ---------------------------------------------------------------------------
# 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 wheels 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/*

# ---------------------------------------------------------------------------
# end-to-end migration pipeline
# ---------------------------------------------------------------------------

# full sequence: extract SSIS packages from the source server, convert the
# .dtsx tree to split-per-flow stored procedures (+ per-directory
# orchestrators), generate one msdb Agent-job script per orchestrator, then
# load the generated procs onto the server with load-stored-procedures.
# NOTE: load-stored-procedures loads onto the SOURCE server; for the TARGET
# server use `deploy-stored-procedures --server-role target` instead.
full-pipeline business="awb" env="dev":
    uv run data_dp_ssis2sp --business-name {{business}} --environment-name {{env}} extract-packages
    uv run data_dp_ssis2sp --business-name {{business}} --environment-name {{env}} convert-tree packages stored_procedures --split-by-flow --no-publish
    uv run data_dp_ssis2sp --business-name {{business}} --environment-name {{env}} create-agent-jobs --procs-dir stored_procedures --out-dir stored_procedures/agent_jobs
    uv run data_dp_ssis2sp --business-name {{business}} --environment-name {{env}} load-stored-procedures --procs-dir stored_procedures

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

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