# Use `just <recipe>` to run a recipe
# https://just.systems/man/en/

# Aliases

alias fmt := format

# Open a terminal with the cabinet-app session
[group('dev')]
term-run:
    zellij --layout term.kdl attach {{ zellijSession }} -c

# Kill the cabinet-app session
[group('dev')]
term-kill:
    -zellij delete-session {{ zellijSession }} -f

# Kill and run a terminal with the cabinet-app session
[group('dev')]
term: term-kill term-run

# Open a browser with the application (http/https)
[group('dev')]
open-browser:
    curl -s http://127.0.0.1:8001 > /dev/null && xdg-open http://127.0.0.1:8001 || xdg-open https://127.0.0.1:8001

# Interactive npm watch script selector
[group('dev')]
watch:
    #!/usr/bin/env bash
    set -euo pipefail

    # Define the watch scripts with descriptions
    watch_scripts=(
        "watch: Whitelabel app"
        "watch-custom: TU Graz app"
    )

    # Use fzf to select a script
    selected_script=$(printf '%s\n' "${watch_scripts[@]}" | fzf \
        --height 40% \
        --layout=reverse \
        --border \
        --prompt='Select NPM watch script > ' \
        --preview='echo "Will run: npm run $(echo {} | cut -d: -f1)"' \
        --preview-window=up:1 \
        | cut -d: -f1)

    # Check if a script was selected
    if [[ -n "$selected_script" ]]; then
        # Get the full line for the selected script
        full_text=$(printf '%s\n' "${watch_scripts[@]}" | grep "^$selected_script:")

        # Set Zellij pane name
        if command -v zellij &> /dev/null; then
            zellij action rename-pane "$full_text"
        fi

        echo "Running: npm run $selected_script"
        npm run "$selected_script"
    else
        echo "No script selected. Exiting."
        exit 1
    fi

# Remove all node-modules directories in the toolkit
[group('dev')]
remove-toolkit-node-modules:
    rm ./vendor/toolkit/node_modules -Rf
    rm ./vendor/toolkit/packages/*/node_modules -Rf

# Remove all node-modules directories in the vendor folder recursively
[group('dev')]
remove-vendor-node-modules:
    find ./vendor -type d -name 'node_modules' -prune -exec rm -rf '{}' +

# Run checker
[group('linter')]
check:
    npm run check

# Run fixer
[group('linter')]
fix:
    npm run fix

# Add git commit hashes to the .git-blame-ignore-revs file
[group('linter')]
add-git-blame-ignore-revs:
    git log --pretty=format:"%H" --grep="^lint" >> .git-blame-ignore-revs
    sort .git-blame-ignore-revs | uniq > .git-blame-ignore-revs.tmp
    mv .git-blame-ignore-revs.tmp .git-blame-ignore-revs

# Format all files using pre-commit
[group('linter')]
format args='':
    pre-commit run --all-files {{ args }}
