set allow-duplicate-variables
set allow-duplicate-recipes
set shell := ["bash", "-euo", "pipefail", "-c"]
set unstable

# ---------------------------------------------------------------------------- #
#                                 DEPENDENCIES                                 #
# ---------------------------------------------------------------------------- #

# Bun: https://bun.sh
bun := require("bun")

# UV: https://github.com/astral-sh/uv
uv := require("uv")

# ---------------------------------------------------------------------------- #
#                                   CONSTANTS                                  #
# ---------------------------------------------------------------------------- #

prettier := "bunx --no-install prettier"
prettier_cache := ".cache/prettier/.prettier-cache"
prettier_globs := "\"**/*.{md,json,jsonc,yaml,yml}\""

# ---------------------------------------------------------------------------- #
#                                   COMMANDS                                   #
# ---------------------------------------------------------------------------- #

# Show available commands
default:
    @just --list

# Install all dependencies for .claude
install:
    bun install
    uv sync --all-extras --dev
    just install-utils

# Install CLI utilities (skipped in CI)
[script]
install-utils:
    if [ "$CI" = "true" ]; then
        echo "Skipping brew install in CI environment"
    else
        brew install bat delta eza fd fzf gh gum jq just rg ruff uv yq
    fi

# ---------------------------------------------------------------------------- #
#                                   HELPERS                                    #
# ---------------------------------------------------------------------------- #

# Add Bash permission to project's local Claude settings (run from project dir)
[no-cd]
[group("helpers")]
@allow-bash:
    uv run helpers/allow_all_bash.py
alias ab := allow-bash

# Clean ~/.claude.json by removing conversation history
[group("helpers")]
@cleanup:
    uv run helpers/cleanup.py

# Merge JSONC settings files into settings.json
[group("helpers")]
@merge-settings:
    gum spin --spinner dot --title "Merging JSONC settings..." -- bash -c './helpers/merge_settings.sh'
alias ms := merge-settings

# Sync a section from template across projects (default: ## Lint Rules)
[group("helpers")]
sync-section section="":
    uv run helpers/sync_context_section.py --section "{{ section }}"
alias ss := sync-section

# ---------------------------------------------------------------------------- #
#                                    CHECKS                                    #
# ---------------------------------------------------------------------------- #

# Run all code checks
[group("checks")]
@full-check:
    just rws prettier-check
    just rws ruff-check
    just rws pyright-check
    echo ""
    echo -e '{{ GREEN }}All code checks passed!{{ NORMAL }}'
alias fc := full-check

# Run all code fixes
[group("checks")]
@full-write:
    just rws prettier-write
    just rws ruff-write
    echo ""
    echo -e '{{ GREEN }}All code fixes applied!{{ NORMAL }}'
alias fw := full-write

# Check documentation and configuration formatting (exclusions in .prettierignore)
[group("checks")]
@prettier-check +globs=prettier_globs:
    {{ prettier }} \
        --check \
        --cache \
        --cache-location {{ prettier_cache }} \
        --log-level warn \
        --no-error-on-unmatched-pattern \
        {{ globs }}
alias pc := prettier-check

# Format documentation and configuration (exclusions in .prettierignore)
[group("checks")]
@prettier-write +globs=prettier_globs:
    {{ prettier }} \
        --write \
        --cache \
        --cache-location {{ prettier_cache }} \
        --log-level warn \
        --no-error-on-unmatched-pattern \
        {{ globs }}
alias pw := prettier-write

# Run staged-file checks
[group("checks")]
@pre-commit:
    sh .husky/pre-commit

alias precommit := pre-commit

# Check Python type hints
[group("checks")]
@pyright-check:
    uv run pyright
alias pyc := pyright-check

# Check Python files
[group("checks")]
@ruff-check:
    uv run ruff check .
alias rc := ruff-check

# Format Python files
[group("checks")]
@ruff-write:
    uv run ruff check --fix .
    uv run ruff format .
alias rw := ruff-write

# ---------------------------------------------------------------------------- #
#                                     TESTS                                    #
# ---------------------------------------------------------------------------- #

# Run all tests
[group("test")]
@test *args:
    uv run pytest hooks helpers {{ args }}
alias t := test

# Run pytest tests
[group("test")]
@test-hooks *args:
    uv run pytest hooks {{ args }}
alias th := test-hooks

# ---------------------------------------------------------------------------- #
#                                   UTILITIES                                  #
# ---------------------------------------------------------------------------- #

# Private recipe to run a check with formatted output
@_run_with-status recipe:
    echo ""
    echo -e '{{ CYAN }}→ Running {{ recipe }}...{{ NORMAL }}'
    just {{ recipe }}
    echo -e '{{ GREEN }}✓ {{ recipe }} completed{{ NORMAL }}'
alias rws := _run_with-status
