# Build Commands Module
# Commands for building, serving, and managing production/development builds
# Set shell for commands

set shell := ["bash", "-c"]

# Build for production (unified pipeline) - default
[no-cd]
prod:
    @echo "🚀 Building for production..."
    @echo "📋 Step 1: Validating content consistency..."
    @just ../dev::content-validate-consistency
    @echo "🎨 Step 2: Building CSS and assets..."
    @just ../dev::css-build
    @echo "📝 Step 3: Generating content indices..."
    @just ../dev::content-generate-indices
    @echo "🏗️  Step 4: Building application with content-static..."
    cargo leptos build --features content-static --release
    @echo "✅ Production build complete!"
    @echo "📁 Output: target/site/"
    @echo "🚀 Run with: cargo run --features content-static --release"

# Build for development (fast)
[no-cd]
dev:
    @echo "🚀 Building for development..."
    # @just ../dev::css-build
    # @just ../dev::content-generate-indices
    cargo leptos build --features content-static
    @echo "✅ Development build complete!"

# Serve built application in production mode
[no-cd]
serve-prod:
    @echo "🚀 Serving application in production mode..."
    @echo "📁 Content source: ${SITE_CONTENT_PATH:-site/content} (runtime processing)"
    @echo "🔄 Features: content-static (optimized)"
    @echo "🏁 Built app: /target/site"
    cargo run --features content-static --release

# Serve with full CMS features (database + API)
[no-cd]
serve-cms:
    @echo "🚀 Serving application with full CMS features..."
    @echo "📁 Content source: ${SITE_CONTENT_PATH:-site/content} + Database"
    @echo "🔄 Features: content-db (includes API + WebSocket)"
    @echo "🎛️ Admin interface available"
    cargo run --features content-db --release

# Build the project for development
[no-cd]
basic:
    @echo "🔨 Building project for development..."
    cargo leptos build

# Build everything (CSS + Content)
all:
    @echo "🔨 Building all assets..."
    @just ../dev::css-build
    @just ../dev::content-build

# Build the project and leptos serve
[no-cd]
serve:
    @echo "🔨 Building project and Leptos serve..."
    cargo leptos serve

# Build the project for production (legacy - use 'just build-prod' instead)
prod-legacy:
    @echo "⚠️  DEPRECATED: Building project for production with legacy script..."
    @echo "💡 Use 'just build::prod' instead for the unified pipeline"
    {{ justfile_directory() }}/scripts/build/leptos-build.sh

# Build with specific features
[no-cd]
features features:
    @echo "🔨 Building with features: {{ features }}..."
    cargo leptos build --features {{ features }}

# Build the project with Cargo
[no-cd]
cargo *ARGS:
    @echo "🔨 Building project with Cargo..."
    cargo build {{ ARGS }}

# Clean build artifacts
[no-cd]
clean:
    @echo "🧹 Cleaning build artifacts..."
    cargo clean
    rm -rf target/
    rm -rf node_modules/

# =============================================================================
# CONTENT CONVERSION COMMANDS
# =============================================================================

# Build markdown converter binary
[no-cd]
content-converter:
    @echo "🔧 Building markdown converter with content-static feature..."
    cargo build --bin markdown_converter --features content-static

# =============================================================================
# DISTRIBUTION COMMANDS
# =============================================================================

# Assemble a transportable distro tarball from provisioning/distro.ncl — the PV
# payload (site_tree) plus the active profile's artifacts (Leptos pkg/, or none
# for htmx-ssr), normalised into the /var/www/site layout. Profile auto-detected
# from rendering.ncl unless given. Supersedes dist-pack (stale dist-list-files).
[no-cd]
distro profile="":
    @nu {{ justfile_directory() }}/scripts/build/distro.nu {{ profile }}

# DEPRECATED: use `just build::distro`. dist-list-files drifted from the layout.
dist-pack-nu target:
    @echo "⚠️  DEPRECATED: use 'just build::distro' (reads provisioning/distro.ncl)"
    nu {{ justfile_directory() }}/scripts/build/dist-pack.nu {{ target }}

# DEPRECATED: use `just build::distro`.
dist-pack target:
    @echo "⚠️  DEPRECATED: use 'just build::distro' (reads provisioning/distro.ncl)"
    {{ justfile_directory() }}/scripts/build/dist-pack.sh {{ target }}

# Cross-platform build (Nushell version)
cross-nu:
    @echo "📦 Build for linux/amd64 and pack Project for distribution (Nushell)..."
    nu {{ justfile_directory() }}/scripts/build/cross-build.nu

# Cross-platform build (Bash fallback)
cross:
    @echo "📦 Build for linux/amd64 and pack Project for distribution (Bash)..."
    {{ justfile_directory() }}/scripts/build/cross-build.sh

# =============================================================================
# DOCKER BUILD COMMANDS
# =============================================================================

# Build Docker image (context assembly + lamina pre-flight via ctx-test.nu)
docker:
    nu {{ justfile_directory() }}/lian-build/ctx-test.nu --run

# Build Docker cross-compilation image (Nushell version)
docker-cross-nu:
    @echo "🐳 Building Docker cross-compilation image (Nushell)..."
    nu {{ justfile_directory() }}/scripts/build/build-docker-cross.nu

# Build Docker image for development
docker-dev:
    @echo "🐳 Building Docker development image..."
    docker build -f Dockerfile.dev -t rustelo:dev .

# =============================================================================
# DEPENDENCY SYNC - rustelo registry → this project's workspace.dependencies
# =============================================================================
# The single source of truth is `{{RUSTELO_ROOT}}/registry/Cargo.toml` (editable in
# Zed via rust-analyzer's inline upgrade hints). These recipes pull the
# canonical versions into this project's managed Cargo.toml region without
# leaving the project root. Overlay (features + extras) comes from this
# project's `rustelo-deps-overlay.toml`.
#
# CARGO_TARGET_DIR override matches the pre-commit hook: keeps the xtask build
# off the framework's volume-pinned target dir when that volume is unmounted.

# Pull canonical workspace deps from rustelo into this project's Cargo.toml.
[no-cd]
deps-sync:
    @echo "🔗 Syncing Cargo.toml from {{RUSTELO_ROOT}}/registry/Cargo.toml..."
    CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target/xtask}" \
        cargo run --manifest-path {{RUSTELO_ROOT}}/xtask/Cargo.toml --quiet -- \
        sync-deps --rustelo-root {{RUSTELO_ROOT}} --target .

# Verify this project's Cargo.toml matches rustelo's registry (exit 1 if drifted).
[no-cd]
deps-sync-check:
    @echo "🔍 Checking Cargo.toml for drift from {{RUSTELO_ROOT}}/registry..."
    CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target/xtask}" \
        cargo run --manifest-path {{RUSTELO_ROOT}}/xtask/Cargo.toml --quiet -- \
        sync-deps --check --rustelo-root {{RUSTELO_ROOT}} --target .
