# justfile — front-end for the greenlight service
# https://just.systems/

set shell := ["mise", "exec", "--", "bash", "-euo", "pipefail", "-c"]
set positional-arguments

# Show available recipes
default:
    @just --list

# Install dependencies into the project venv
setup:
    uv sync

# Run the greenlight CLI
run *args:
    uv run greenlight "$@"

# Scan open PRs from trusted authors in pytorch/pytorch; dispatch the reviewer workflow for new/changed PRs
review *args:
    uv run greenlight review "$@"

# Type-check source and tests, plus the greenlight-owned detector hook
typecheck:
    uv run mypy
    uv run mypy ../.claude/hooks/greenlight/assert-loaded-instructions.py

# Run the test suite with coverage
test:
    uv run pytest -n auto --cov=greenlight --cov-report=term-missing:skip-covered --cov-report=json

# Run all linters in check mode, aggregating every failure before exiting
lint:
    #!/usr/bin/env bash
    set -euo pipefail
    source scripts/mise-activate.sh

    FAILED=()
    _run() {
        local name="$1"
        shift
        echo "━━━ ${name} ━━━"
        if "$@"; then
            echo "  ok"
        else
            echo "  FAILED"
            FAILED+=("${name}")
        fi
    }

    _run "ruff check" ruff check --config pyproject.toml . ../.claude/hooks/greenlight/
    _run "ruff format" ruff format --check --config pyproject.toml . ../.claude/hooks/greenlight/
    _run "yamllint" uv run yamllint .
    _run "taplo check" taplo check
    _run "shellcheck" shellcheck --rcfile .shellcheckrc scripts/*.sh ../.claude/hooks/greenlight/*.sh
    _run "shfmt" shfmt -d -i 2 -ci scripts
    _run "markdownlint" markdownlint-cli2 "**/*.md" "#.venv" "#build" "#dist"

    echo ""
    if [[ ${#FAILED[@]} -gt 0 ]]; then
        echo "FAILED: ${FAILED[*]}"
        exit 1
    fi
    echo "All lint checks passed."

# Auto-fix lint issues where possible
lint-fix:
    #!/usr/bin/env bash
    set -euo pipefail
    source scripts/mise-activate.sh

    ruff check --fix .
    ruff format .
    shfmt -w -i 2 -ci scripts
    taplo fmt
    markdownlint-cli2 --fix "**/*.md" "#.venv" "#build" "#dist" || true

# Build the AWS Lambda deployment zip (dist/greenlight-scan.zip) with linux x86_64 / cp313 wheels
package:
    #!/usr/bin/env bash
    set -euo pipefail
    source scripts/mise-activate.sh

    repo="$(pwd)"
    build_dir="${repo}/build/lambda"
    reqs="${repo}/build/requirements-lambda.txt"
    zip_path="${repo}/dist/greenlight-scan.zip"

    rm -rf "${build_dir}" "${zip_path}"
    mkdir -p "${build_dir}" "${repo}/dist"

    uv export --no-dev --no-emit-project --no-hashes --frozen --format requirements.txt -o "${reqs}"

    uv run --with pip -- python -m pip install \
        -r "${reqs}" \
        -t "${build_dir}" \
        --platform manylinux2014_x86_64 \
        --platform manylinux_2_28_x86_64 \
        --only-binary=:all: \
        --python-version 3.13 \
        --implementation cp

    cp -a "${repo}/src/greenlight" "${build_dir}/greenlight"
    find "${build_dir}" -name __pycache__ -type d -prune -exec rm -rf {} +

    (cd "${build_dir}" && zip -q -r "${zip_path}" .)

    echo "Built ${zip_path} ($(du -h "${zip_path}" | cut -f1))"

# Remove caches, build artifacts, and the venv
clean:
    #!/usr/bin/env bash
    set -euo pipefail
    rm -rf .venv .pytest_cache .ruff_cache .mypy_cache .coverage coverage.json htmlcov dist build src/*.egg-info
    find . -name __pycache__ -type d -prune -exec rm -rf {} +
