import "./settings.just"
import "./utils.just"

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

# Ni: https://github.com/antfu-collective/ni
na := require("na")
ni := require("ni")
nlx := require("nlx")

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

GLOBS_PRETTIER := "\"**/*.{md,mdx,yaml,yml}\""

# ---------------------------------------------------------------------------- #
#                                    RECIPES                                   #
# ---------------------------------------------------------------------------- #

# Clean files
[no-cd]
clean:
    just _clean ".DS_Store"

[no-cd]
_clean +globs:
    nlx del-cli "{{ globs }}"

# Clear node_modules recursively
[confirm("Are you sure you want to delete all node_modules, including in subdirectories? [y/N]"), no-cd]
@clean-modules +globs="**/node_modules":
    echo "🧹 Deleting node_modules recursively..."
    nlx del-cli --verbose {{ globs }}

# Install the Node.js dependencies; run with --frozen to install the dependencies with the frozen lockfile
[no-cd]
@install *args:
    ni {{ args }}

# Build with TypeScript
[no-cd]
[arg("project", long, short="p", help="Path to tsconfig.json")]
@tsc-build project="tsconfig.json":
    na tsc -p {{ project }}
alias tb := tsc-build

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

# Check code with Biome - runs both the checker and the linter
[group("checks"), no-cd]
@biome-check +globs=".":
    na biome check {{ globs }}
alias bc := biome-check

# Lint code with Biome
[group("checks"), no-cd]
@biome-lint +globs=".":
    na biome lint {{ globs }}
alias bl := biome-lint

# Fix code with Biome
# The `noUnusedImports` rule is disabled by default to allow unused imports during development
[group("checks"), no-cd]
@biome-write +globs=".":
    na biome check --write {{ globs }}
    na biome lint --unsafe --write --only correctness/noUnusedImports {{ globs }}
alias bw := biome-write

# Run all code checks
[group("checks"), no-cd]
@_full-check:
    just _run-with-status biome-check
    just _run-with-status prettier-check
    just _run-with-status type-check

# Run all code checks
[group("checks"), no-cd]
@full-check: _full-check
    echo ""
    echo '{{ GREEN }}All code checks passed!{{ NORMAL }}'
alias fc := full-check

# Run all code fixes
[group("checks"), no-cd]
@_full-write:
    just _run-with-status biome-write
    just _run-with-status prettier-write

# Run all code fixes
[group("checks"), no-cd]
@full-write: _full-write
    echo ""
    echo '{{ GREEN }}All code fixes applied!{{ NORMAL }}'
alias fw := full-write

# Run knip checks
[group("checks"), no-cd]
@knip-check:
    na knip
alias kc := knip-check

# Run knip fix
[group("checks"), no-cd]
@knip-write:
    na knip --fix
alias kw := knip-write

# Check Prettier formatting
[group("checks"), no-cd]
@prettier-check +globs=GLOBS_PRETTIER:
    na prettier \
        --check \
        --cache \
        --log-level warn \
        --no-error-on-unmatched-pattern \
        {{ globs }}
alias pc := prettier-check

# Format using Prettier
[group("checks"), no-cd]
@prettier-write +globs=GLOBS_PRETTIER:
    na prettier \
        --write \
        --cache \
        --log-level warn \
        --no-error-on-unmatched-pattern \
        {{ globs }}
alias pw := prettier-write

# Type check with TypeScript (tsc — TypeScript 7's native compiler)
[group("checks"), no-cd]
[arg("project", long, short="p", help="Path to tsconfig.json")]
type-check project="tsconfig.json":
    na tsc --noEmit --project {{ project }}
alias tc := type-check
alias tsc-check := type-check
