# ╔══════════════════════════════════════════════════════════════════════╗
# ║                      NUSHELL SCRIPTS                                 ║
# ║                   Maintenance and automation scripts                 ║
# ╚══════════════════════════════════════════════════════════════════════╝

# === PATHS ===
SCRIPTS_DIR := "scripts"

# Help for scripts module
help:
    @echo "NUSHELL SCRIPTS MODULE"
    @echo ""
    @echo "Sync and backup:"
    @echo "  just scripts::sync              Sync filesystem with SurrealDB"
    @echo "  just scripts::backup            Backup knowledge base"
    @echo "  just scripts::reindex           Rebuild embeddings index"
    @echo ""
    @echo "Import/Export:"
    @echo "  just scripts::import-logseq DIR Import from Logseq graph"
    @echo "  just scripts::export-logseq DIR Export to Logseq format"
    @echo ""
    @echo "Statistics:"
    @echo "  just scripts::stats             Show KOGRAL statistics"
    @echo "  just scripts::stats-json        Show stats in JSON format"
    @echo ""
    @echo "Maintenance:"
    @echo "  just scripts::migrate           Run schema migrations"
    @echo "  just scripts::check-scripts     Validate NuShell scripts"

# === SYNC AND BACKUP ===

# Sync filesystem with SurrealDB
[doc("Sync filesystem with SurrealDB storage")]
sync DIRECTION="bidirectional":
    @echo "=== Syncing KOGRAL ({{ DIRECTION }}) ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-sync.nu --direction {{ DIRECTION }}; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# Backup knowledge base
[doc("Backup knowledge base to archive")]
backup:
    @echo "=== Backing up KOGRAL ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-backup.nu --format tar --compress; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# Rebuild embeddings index
[doc("Rebuild embeddings index")]
reindex PROVIDER="fastembed":
    @echo "=== Reindexing embeddings ({{ PROVIDER }}) ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-reindex.nu --provider {{ PROVIDER }}; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# === IMPORT/EXPORT ===

# Import from Logseq graph
[doc("Import from Logseq graph directory")]
import-logseq DIR:
    @echo "=== Importing from Logseq: {{ DIR }} ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-import-logseq.nu "{{ DIR }}"; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# Export to Logseq format
[doc("Export to Logseq format")]
export-logseq DIR:
    @echo "=== Exporting to Logseq: {{ DIR }} ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-export-logseq.nu "{{ DIR }}"; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# === STATISTICS ===

# Show KOGRAL statistics (summary format)
[doc("Show knowledge base statistics")]
stats:
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-stats.nu --format summary --show-tags; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# Show KOGRAL statistics in JSON format
[doc("Show knowledge base statistics (JSON)")]
stats-json:
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-stats.nu --format json; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# === MAINTENANCE ===

# Run schema migrations
[doc("Run database schema migrations")]
migrate TARGET="latest":
    @echo "=== Running migrations (target: {{ TARGET }}) ==="
    @if command -v nu >/dev/null 2>&1; then \
        nu {{ SCRIPTS_DIR }}/kogral-migrate.nu --target {{ TARGET }}; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi

# Validate NuShell scripts syntax
[doc("Validate NuShell scripts")]
check-scripts:
    @echo "=== Validating NuShell scripts ==="
    @if command -v nu >/dev/null 2>&1; then \
        for script in {{ SCRIPTS_DIR }}/*.nu; do \
            echo "Checking $$script..."; \
            nu -c "source $$script; help" >/dev/null 2>&1 || { echo "✗ $$script has syntax errors"; exit 1; }; \
        done; \
        echo "✓ All scripts valid"; \
    else \
        echo "Error: nushell not installed"; \
        exit 1; \
    fi
