# =============================================================================
# jpl-website - Unified Template System Justfile
# =============================================================================
# Generated by cargo rustelo v0.1.0 using unified template system
# Template: {{template_name}} | Features: {{#each features}}{{this}} {{/each}}
#
# This justfile supports layered override system with feature-aware commands.
# Layer precedence: Local > Feature > Template > Framework
#
# IMPORTANT: This file is managed by the template system.
# Local customizations should go in config/local/justfile (highest precedence).

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

# Import local overrides from layered system
mod? local 'config/local/justfile'
{{#each features}}
mod? {{this}} 'config/features/{{this}}/justfile'
{{/each}}

# =============================================================================
# CORE PROJECT TASKS
# =============================================================================

# Show available tasks (default)
default:
    @echo "📋 jpl-website - Available Tasks:"
    @just --list

# Start development server with hot reload
dev:
    @echo "🚀 Starting development server..."
    cargo rustelo dev

# Build for development
build:
    @echo "🔨 Building for development..."
    cargo rustelo build

# Build for production (optimized)
build-prod:
    @echo "🔨 Building for production..."
    cargo rustelo build --release

# Run tests
test:
    @echo "🧪 Running tests..."
    cargo test

# Check code quality (lint, format, etc.)
check:
    @echo "🔍 Checking code quality..."
    cargo check
    cargo clippy
    cargo fmt --check

# Format code
fmt:
    @echo "✨ Formatting code..."
    cargo fmt

# Clean build artifacts
clean:
    @echo "🧹 Cleaning build artifacts..."
    cargo clean
    rm -rf target/
    rm -rf dist/

# Install/update dependencies
deps:
    @echo "📦 Installing dependencies..."
    cargo fetch
    npm install

# =============================================================================
# SETUP AND INITIALIZATION TASKS
# =============================================================================

# Initial project setup
setup:
    @echo "🔧 Setting up jpl-website..."
    @if [ -f "scripts/setup/install.sh" ]; then \
        chmod +x scripts/setup/*.sh && ./scripts/setup/install.sh; \
    else \
        echo "⚠️  No setup script found, running basic setup..."; \
        just deps; \
    fi

# Setup development environment
setup-dev:
    @echo "🛠️  Setting up development environment..."
    @if [ -f "scripts/setup/setup_dev.sh" ]; then \
        chmod +x scripts/setup/setup_dev.sh && ./scripts/setup/setup_dev.sh; \
    else \
        just setup; \
    fi

# =============================================================================
# ASSET AND BUILD PIPELINE TASKS
# =============================================================================

# List asset resolution paths
assets-list:
    @echo "📁 Asset resolution paths:"
    cargo rustelo assets list

# Check for asset conflicts
assets-check:
    @echo "🔍 Checking for asset conflicts..."
    cargo rustelo assets check

# Organize assets by category
assets-organize:
    @echo "📂 Organizing assets by category..."
    cargo rustelo assets organize ./dist

# Update assets from remote source
assets-update:
    @echo "⬆️  Updating assets from source..."
    cargo rustelo assets update

# =============================================================================
# DATABASE TASKS (if enabled)
# =============================================================================

# Setup database
db-setup:
    @echo "🗄️  Setting up database..."
    @if [ -f "scripts/database/db-setup.sh" ]; then \
        chmod +x scripts/database/db-setup.sh && ./scripts/database/db-setup.sh; \
    else \
        echo "⚠️  No database setup script found"; \
    fi

# Run database migrations
db-migrate:
    @echo "🔄 Running database migrations..."
    @if [ -f "scripts/database/db-migrate.sh" ]; then \
        chmod +x scripts/database/db-migrate.sh && ./scripts/database/db-migrate.sh; \
    else \
        echo "⚠️  No migration script found"; \
    fi

# Backup database
db-backup:
    @echo "💾 Creating database backup..."
    @if [ -f "scripts/database/db-backup.sh" ]; then \
        chmod +x scripts/database/db-backup.sh && ./scripts/database/db-backup.sh; \
    else \
        echo "⚠️  No backup script found"; \
    fi

# =============================================================================
# FRAMEWORK AND UPDATE TASKS
# =============================================================================

# Update framework dependencies (safe)
update:
    @echo "⬆️  Updating framework dependencies..."
    cargo rustelo update

# Update with interactive conflict resolution
update-interactive:
    @echo "⬆️  Interactive framework update..."
    cargo rustelo update --interactive

# Show framework and project information
info:
    @echo "📋 Project Information:"
    @echo "  Name: jpl-website"
    @echo "  Template: content-website"
    @echo "  Generated by: cargo rustelo v0.1.0"
    @echo "  Generation time: 2025-10-28T18:46:17.145459+00:00"
    @echo "  Rust version: $(rustc --version)"
    @echo "  Cargo version: $(cargo --version)"
    @echo "  Node version: $(node --version 2>/dev/null || echo 'Not installed')"
    @echo "  Project root: $(pwd)"

# =============================================================================
# DEPLOYMENT AND CROSS-COMPILATION
# =============================================================================

# Initialize cross-compilation for Linux
cross-init:
    @echo "🐋 Initializing cross-compilation..."
    @if [ -f "scripts/build/cross-build.sh" ]; then \
        chmod +x scripts/build/cross-build.sh; \
    fi
    cargo rustelo cross init

# Build for Linux using Docker (for deployment)
cross-build:
    @echo "🐋 Cross-compiling for Linux..."
    @if [ -f "scripts/build/cross-build.sh" ]; then \
        ./scripts/build/cross-build.sh; \
    else \
        cargo rustelo cross build; \
    fi

# Deploy to production (customize as needed)
deploy: build-prod
    @echo "🚀 Deploying to production..."
    @if [ -f "scripts/deploy/deploy.sh" ]; then \
        chmod +x scripts/deploy/deploy.sh && ./scripts/deploy/deploy.sh; \
    else \
        echo "⚠️  Customize this task for your deployment target"; \
        echo "📁 Production build available in: target/release/"; \
    fi

# =============================================================================
# TESTING TASKS
# =============================================================================

# Run all tests
test-all:
    @echo "🧪 Running all tests..."
    @if [ -f "scripts/testing/page-browser-tester.sh" ]; then \
        chmod +x scripts/testing/*.sh; \
    fi
    cargo test
    @if [ -d "end2end" ] && command -v npm >/dev/null 2>&1; then \
        echo "🌐 Running end-to-end tests..."; \
        cd end2end && npm test; \
    fi

# Run performance tests
test-performance:
    @echo "⚡ Running performance tests..."
    @if [ -f "scripts/testing/page-browser-tester.sh" ]; then \
        ./scripts/testing/page-browser-tester.sh --performance; \
    else \
        echo "⚠️  No performance testing script found"; \
    fi

# =============================================================================
# FEATURE-SPECIFIC TASKS
# =============================================================================

{{#if features.analytics}}
# === Analytics Commands ===

# Start analytics development server
analytics-dev:
    @echo "📊 Starting analytics development..."
    cargo run --bin analytics -- dev --port 3001

# Generate analytics report
analytics-report hours="24":
    @echo "📈 Generating analytics report for {{hours}} hours..."
    cargo run --bin analytics -- report --hours {{hours}}

# Run analytics dashboard
analytics-dashboard:
    @echo "📋 Starting analytics dashboard..."
    cargo run --bin analytics -- dashboard --refresh 30

# Test analytics functionality
test-analytics:
    @echo "🧪 Testing analytics..."
    cargo test --package analytics --all-features
{{/if}}

{{#if features.smart-build}}
# === Smart Build Commands ===

# Build with smart caching
build-cached:
    @echo "⚡ Building with smart cache..."
    cargo run --bin smart-build -- build --cached

# Clean build cache
cache-clean:
    @echo "🧹 Cleaning build cache..."
    cargo run --bin smart-build -- cache clean

# Show cache statistics
cache-stats:
    @echo "📊 Build cache statistics..."
    cargo run --bin smart-build -- cache stats

# Optimize cache
cache-optimize:
    @echo "⚡ Optimizing build cache..."
    cargo run --bin smart-build -- cache optimize
{{/if}}

{{#if features.debugging-tools}}
# === Debugging Commands ===

# Start debug mode with enhanced logging
dev-debug:
    @echo "🐛 Starting debug mode..."
    RUST_LOG=debug cargo leptos watch

# Watch debug logs
debug-watch:
    @echo "👀 Watching debug logs..."
    cargo run --bin debugging-tools -- watch --verbose

# Capture browser logs
debug-browser page="/":
    @echo "🌐 Capturing browser logs for {{page}}..."
    cargo run --bin debugging-tools -- browser {{page}}

# Full debugging session
debug-full page="/":
    @echo "🔍 Starting full debugging session..."
    cargo run --bin debugging-tools -- full-debug {{page}}
{{/if}}

{{#if features.content-static}}
# === Content Management Commands ===

# Build static content
content-build:
    @echo "📝 Building static content..."
    cargo run --bin tools -- content build

# Watch content changes
content-watch:
    @echo "👀 Watching content changes..."
    cargo run --bin tools -- content watch

# Validate content
content-validate:
    @echo "✅ Validating content..."
    cargo run --bin tools -- content validate
{{/if}}

# === CSS and Styling Commands ===

# Build CSS with UnoCSS (layered configuration)
css-build:
    @echo "🎨 Building CSS with layered configuration..."
    {{#if has_package_json}}
    npm run css:build
    {{else}}
    cargo run --bin tools -- css build
    {{/if}}

# Watch CSS changes
css-watch:
    @echo "👀 Watching CSS changes..."
    {{#if has_package_json}}
    npm run css:watch
    {{else}}
    cargo run --bin tools -- css watch
    {{/if}}

# Optimize CSS for production
css-optimize:
    @echo "⚡ Optimizing CSS for production..."
    cargo run --bin tools -- css optimize

# === Layered Override Management ===

# List all active overrides
list-overrides:
    @echo "📋 Active layered overrides:"
    cargo rustelo list-overrides

# Create local override
override type name:
    @echo "🎛️ Creating local override for {{type}}: {{name}}"
    cargo rustelo override {{type}} {{name}}

# Trace resolution path
trace type name:
    @echo "🔍 Tracing resolution for {{type}}: {{name}}"
    cargo rustelo trace {{type}} {{name}}

# Remove local override
remove-override type name:
    @echo "🗑️ Removing local override for {{type}}: {{name}}"
    cargo rustelo remove-override {{type}} {{name}} --force

# =============================================================================
# UNIFIED DEVELOPMENT WORKFLOWS
# =============================================================================

# Full development mode (all enabled features)
dev-full:
    @echo "🔥 Starting full development mode..."
    #!/usr/bin/env bash
    set -euo pipefail

    # Start development server in background
    cargo leptos watch &
    LEPTOS_PID=$!

    # Start CSS watcher
    {{#if has_package_json}}
    npm run css:watch &
    CSS_PID=$!
    {{/if}}

    {{#if features.analytics}}
    # Start analytics development server
    just analytics-dev &
    ANALYTICS_PID=$!
    {{/if}}

    {{#if features.debugging-tools}}
    # Start debug log watcher
    just debug-watch &
    DEBUG_PID=$!
    {{/if}}

    echo "🚀 All services started. Press Ctrl+C to stop all."

    # Trap Ctrl+C and kill all background processes
    trap 'kill $LEPTOS_PID {{#if has_package_json}}$CSS_PID{{/if}} {{#if features.analytics}}$ANALYTICS_PID{{/if}} {{#if features.debugging-tools}}$DEBUG_PID{{/if}} 2>/dev/null' INT

    # Wait for all background processes
    wait

# Quality assurance workflow
quality:
    @echo "✨ Running comprehensive quality checks..."
    just fmt
    just check
    just test
    {{#if features.analytics}}
    just test-analytics
    {{/if}}
    {{#if features.smart-build}}
    just cache-stats
    {{/if}}
    cargo audit

# =============================================================================
# HELP AND DOCUMENTATION
# =============================================================================

# Show detailed help for common workflows
help:
    @echo "🚀 jpl-website - Common Workflows:"
    @echo ""
    @echo "🔧 Development:"
    @echo "  just setup        # Initial project setup"
    @echo "  just dev          # Start development server"
    @echo "  just test         # Run tests"
    @echo "  just check        # Check code quality"
    @echo ""
    @echo "🔨 Building:"
    @echo "  just build        # Development build"
    @echo "  just build-prod   # Production build"
    @echo "  just clean        # Clean artifacts"
    @echo ""
    @echo "🗄️  Database:"
    @echo "  just db-setup     # Setup database"
    @echo "  just db-migrate   # Run migrations"
    @echo "  just db-backup    # Backup database"
    @echo ""
    @echo "🎨 Assets:"
    @echo "  just assets-list  # Show asset paths"
    @echo "  just assets-check # Check conflicts"
    @echo "  just assets-update# Update from source"
    @echo ""
    @echo "⬆️  Updates:"
    @echo "  just update       # Safe framework update"
    @echo "  just info         # Show project info"
    @echo ""
    @echo "🚀 Deployment:"
    @echo "  just cross-init   # Setup cross-compilation"
    @echo "  just cross-build  # Build for Linux"
    @echo "  just deploy       # Deploy to production"
    @echo ""
    @echo "📖 More help:"
    @echo "  cargo rustelo --help     # All available commands"
    @echo "  just --list              # All available tasks"

# Show framework-specific help
help-framework:
    @echo "🦀 Rustelo Framework Help:"
    cargo rustelo --help

# =============================================================================
# LOCAL CUSTOMIZATION NOTES
# =============================================================================
#
# To add local tasks without modifying this file:
# 1. Create 'local-tasks.just' in this directory
# 2. Add your custom tasks there
# 3. They will be automatically imported above
#
# Example local-tasks.just:
# ```
# # Custom deployment task
# deploy-staging:
#     @echo "Deploying to staging..."
#     # Your custom deployment commands
# ```
#
# This approach keeps your customizations separate from the generated file,
# making framework updates safe and preserving your local modifications.
# =============================================================================
