# Utilities Commands Module
# Commands for setup, configuration, monitoring, and maintenance utilities

# Set shell for commands
set shell := ["bash", "-c"]

# =============================================================================
# SETUP COMMANDS
# =============================================================================

# Complete project setup (default)
setup:
    @echo "🔧 Setting up project..."
    {{justfile_directory()}}/scripts/setup/setup_dev.sh

# Setup with custom name
setup-name name:
    @echo "🔧 Setting up project with name: {{name}}..."
    {{justfile_directory()}}/scripts/setup/setup_dev.sh --name {{name}}

# Setup for production
setup-prod:
    @echo "🔧 Setting up project for production..."
    {{justfile_directory()}}/scripts/setup/setup_dev.sh --env prod

# Install system dependencies
setup-deps:
    @echo "🔧 Installing system dependencies..."
    {{justfile_directory()}}/scripts/setup/install-dev.sh

# Setup wizard
setup-wizard:
    @echo "🔧 Setting configuration wizard..."
    {{justfile_directory()}}/scripts/setup/run_wizard.sh

# Setup configuration
setup-config:
    @echo "🔧 Setting up configuration..."
    {{justfile_directory()}}/scripts/setup/setup-config.sh

# Setup encryption
setup-encryption:
    @echo "🔧 Setting up encryption..."
    {{justfile_directory()}}/scripts/setup/setup_encryption.sh

# Generate TLS certificates
setup-tls:
    @echo "🔧 Generating TLS certificates..."
    {{justfile_directory()}}/scripts/utils/generate_certs.sh

# =============================================================================
# MONITORING COMMANDS
# =============================================================================

# Check application health
health:
    @echo "🏥 Checking application health..."
    curl -f http://localhost:3030/health || echo "Health check failed"

# Check readiness
ready:
    @echo "🏥 Checking application readiness..."
    curl -f http://localhost:3030/health/ready || echo "Readiness check failed"

# Check liveness
live:
    @echo "🏥 Checking application liveness..."
    curl -f http://localhost:3030/health/live || echo "Liveness check failed"

# View metrics
metrics:
    @echo "📊 Viewing metrics..."
    curl -s http://localhost:3030/metrics

# View logs
logs:
    @echo "📋 Viewing logs..."
    tail -f logs/app.log

# =============================================================================
# CONFIGURATION COMMANDS
# =============================================================================

# Show configuration
config:
    @echo "⚙️ Configuration:"
    @cat .env 2>/dev/null || echo "No .env file found"

# Encrypt configuration value
encrypt value:
    @echo "🔒 Encrypting value..."
    cargo run --bin config_crypto_tool encrypt "{{value}}"

# Decrypt configuration value
decrypt value:
    @echo "🔓 Decrypting value..."
    cargo run --bin config_crypto_tool decrypt "{{value}}"

# Test encryption
test-encryption:
    @echo "🔒 Testing encryption..."
    {{justfile_directory()}}/scripts/utils/test_encryption.sh

# =============================================================================
# UTILITY COMMANDS
# =============================================================================

# Install Node.js dependencies
npm-install:
    @echo "📦 Installing Node.js dependencies..."
    npm install

# Install Rust dependencies (check)
cargo-check:
    @echo "📦 Checking Rust dependencies..."
    cargo check

# Update dependencies
update:
    @echo "📦 Updating dependencies..."
    cargo update
    npm update

# Show project information
info:
    @echo "ℹ️ Project Information:"
    @echo "  Rust version: $(rustc --version)"
    @echo "  Cargo version: $(cargo --version)"
    @echo "  Node.js version: $(node --version)"
    @echo "  npm version: $(npm --version)"
    @echo "  Project root: $(pwd)"

# Show disk usage
disk-usage:
    @echo "💾 Disk usage:"
    @echo "  Target directory: $(du -sh target/ 2>/dev/null || echo 'N/A')"
    @echo "  Node modules: $(du -sh node_modules/ 2>/dev/null || echo 'N/A')"

# Check system requirements
check-requirements:
    @echo "✅ Checking system requirements..."
    @echo "Rust: $(rustc --version 2>/dev/null || echo 'rust Not installed')"
    @echo "Cargo: $(cargo --version 2>/dev/null || echo 'cargo Not installed')"
    @echo "Node.js: $(node --version 2>/dev/null || echo 'node Not installed')"
    @echo "pnpm: $(pnpm --version 2>/dev/null || echo 'pnpm Not installed')"
    @echo "mdbook: $(mdbook --version 2>/dev/null || echo 'mdbook Not installed')"
    @echo "Docker: $(docker --version 2>/dev/null || echo 'docker Not installed')"
    @echo "PostgreSQL: $(psql --version 2>/dev/null || echo 'psql for PostgreSQL Not installed')"
    @echo "SQLite: $(sqlite3 --version 2>/dev/null || echo 'sqlite3 Not installed')"

# =============================================================================
# SCRIPT MANAGEMENT COMMANDS
# =============================================================================

# Make all scripts executable
scripts-executable:
    @echo "🔧 Making all scripts executable..."
    {{justfile_directory()}}/scripts/others/make-executable.sh

# Make all scripts executable with verbose output
scripts-executable-verbose:
    @echo "🔧 Making all scripts executable (verbose)..."
    {{justfile_directory()}}/scripts/others/make-executable.sh --verbose

# List all available scripts
scripts-list:
    @echo "📋 Available scripts:"
    @echo ""
    @echo "🗄️ Database Scripts:"
    @ls -la scripts/databases/*.sh 2>/dev/null || echo "  No database scripts found"
    @echo ""
    @echo "🔧 Setup Scripts:"
    @ls -la scripts/setup/*.sh 2>/dev/null || echo "  No setup scripts found"
    @echo ""
    @echo "🛠️ Tool Scripts:"
    @ls -la scripts/tools/*.sh 2>/dev/null || echo "  No tool scripts found"
    @echo ""
    @echo "🔧 Utility Scripts:"
    @ls -la scripts/utils/*.sh 2>/dev/null || echo "  No utility scripts found"

# Check script permissions
scripts-check:
    @echo "🔍 Checking script permissions..."
    @find scripts -name "*.sh" -type f ! -executable -exec echo "❌ Not executable: {}" \; || echo "✅ All scripts are executable"

# =============================================================================
# MAINTENANCE COMMANDS
# =============================================================================

# Clean everything
clean-all:
    @echo "🧹 Cleaning everything..."
    @just ../build::clean
    rm -rf logs/
    rm -rf backups/
    docker system prune -f

# Backup project
backup:
    @echo "💾 Creating project backup..."
    @just ../database::backup
    tar -czf backup-$(date +%Y%m%d-%H%M%S).tar.gz \
        --exclude=target \
        --exclude=node_modules \
        --exclude=.git \
        .

# Show comprehensive system overview
overview:
    @echo "🔍 Running system overview..."
    {{justfile_directory()}}/scripts/setup/overview.sh
