# ╔══════════════════════════════════════════════════════════════════════╗
# ║                      BUILD RECIPES                                   ║
# ║            Build workspace with different feature flags              ║
# ╚══════════════════════════════════════════════════════════════════════╝

# === FEATURE FLAGS ===
FEATURES_CLI := "cli,i18n,templates"
FEATURES_TUI := "tui,i18n,templates"
FEATURES_WEB := "web,i18n,templates"
FEATURES_AI := "ai_backend"
FEATURES_AGENT := "markup,nickel,cache"
FEATURES_ALL_BACKENDS := "cli,tui,web"
FEATURES_FULL := "cli,tui,web,i18n,templates"
FEATURES_FULL_AI := "cli,tui,web,i18n,templates,ai_backend"

# Help for build module
help:
    @echo "BUILD MODULE"
    @echo ""
    @echo "Build default (CLI + i18n + templates):"
    @echo "  just build::default"
    @echo ""
    @echo "Build specific backends:"
    @echo "  just build::cli         Build CLI backend (rendering)"
    @echo "  just build::tui         Build TUI backend (rendering)"
    @echo "  just build::web         Build Web backend (rendering)"
    @echo "  just build::ai-lib      Build AI backend (library: RAG, KG, embeddings)"
    @echo "  just build::ai          Build AI backend (FormBackend + microservice)"
    @echo "  just build::agent       Build Agent system (LLM execution)"
    @echo "  just build::agent-server Build Agent server (HTTP API)"
    @echo "  just build::prov-gen    Build Provisioning generator (IaC)"
    @echo ""
    @echo "Build combined:"
    @echo "  just build::all         Build all backends"
    @echo "  just build::all-backends Build all in one"
    @echo "  just build::full        Build all features"
    @echo ""
    @echo "Build release:"
    @echo "  just build::release     Release build (all features)"
    @echo ""
    @echo "Check compilation:"
    @echo "  just build::check       Check without building"

# === DEFAULT BUILD ===

# Build workspace with default features
[doc("Build default: CLI + i18n + templates")]
default:
    @echo "=== Building default features ==="
    cargo build --workspace
    @echo "✓ Default build complete"

# === INDIVIDUAL BACKENDS ===

# Build CLI backend only
[doc("Build CLI backend")]
cli:
    @echo "=== Building CLI backend ==="
    cargo build -p typedialog-core --features "{{ FEATURES_CLI }}"
    cargo build -p typedialog
    @echo "✓ CLI backend ready"

# Build TUI backend only
[doc("Build TUI backend")]
tui:
    @echo "=== Building TUI backend ==="
    cargo build -p typedialog-core --features "{{ FEATURES_TUI }}"
    cargo build -p typedialog-tui
    @echo "✓ TUI backend ready"

# Build Web backend only
[doc("Build Web backend")]
web:
    @echo "=== Building Web backend ==="
    cargo build -p typedialog-core --features "{{ FEATURES_WEB }}"
    cargo build -p typedialog-web
    @echo "✓ Web backend ready"

# Build AI backend library (RAG, Knowledge Graph, embeddings in core)
[doc("Build AI backend library")]
ai-lib:
    @echo "=== Building AI backend library ==="
    cargo build -p typedialog-core --features "{{ FEATURES_AI }}"
    @echo "✓ AI backend library ready (RAG, KG, embeddings)"

# Build AI backend (FormBackend implementation + microservice)
[doc("Build AI backend (FormBackend + microservice)")]
ai:
    @echo "=== Building AI backend ==="
    cargo build -p typedialog-ai
    @echo "✓ AI backend ready (FormBackend + REST API + WebSocket + Web UI)"

# Build Agent system (CLI for LLM execution)
[doc("Build Agent system (typedialog-ag CLI)")]
agent:
    @echo "=== Building Agent system ==="
    cargo build -p typedialog-ag-core --features "{{ FEATURES_AGENT }}"
    cargo build -p typedialog-ag
    @echo "✓ Agent system ready (typedialog-ag CLI)"

# Build Agent with server support (same binary, use 'typedialog-ag serve')
[doc("Build Agent (use 'typedialog-ag serve' for HTTP server)")]
agent-server:
    @echo "=== Building Agent (with server support) ==="
    cargo build -p typedialog-ag-core --features "{{ FEATURES_AGENT }}"
    cargo build -p typedialog-ag
    @echo "✓ Agent ready (use 'typedialog-ag serve' to start HTTP server)"

# Build Provisioning generator (Infrastructure as Code)
[doc("Build Provisioning generator (typedialog-prov-gen)")]
prov-gen:
    @echo "=== Building Provisioning generator ==="
    cargo build -p typedialog-prov-gen
    @echo "✓ Provisioning generator ready (IaC generation)"

# === COMBINED BUILDS ===

# Build all backends (cli, tui, web)
[doc("Build all backends together")]
all-backends:
    @echo "=== Building all backends ==="
    cargo build -p typedialog-core --features "{{ FEATURES_ALL_BACKENDS }},i18n,templates"
    cargo build -p typedialog
    cargo build -p typedialog-tui
    cargo build -p typedialog-web
    @echo "✓ All backends ready"

# Build everything (all features)
[doc("Build with all features")]
full:
    @echo "=== Building with full feature set ==="
    cargo build --workspace --all-features
    @echo "✓ Full build complete"

# Build all variants (including AI backend, Agent system, and Prov-gen)
[doc("Build all: default + all backends + AI + Agent + Agent-server + Prov-gen + release")]
all:
    just build::default
    just build::all-backends
    just build::ai
    just build::agent
    just build::agent-server
    just build::prov-gen
    just build::release
    @echo "✓ All builds complete"

# === RELEASE BUILD ===

# Release build with all features
[doc("Release build: optimized, all features")]
release:
    @echo "=== Building release (optimized) ==="
    cargo build --workspace --all-features --release
    @echo ""
    @echo "Binaries ready:"
    @ls -lh target/release/typedialog* 2>/dev/null | awk '{print "  " $NF " (" $5 ")"}'
    @echo "✓ Release build complete"

# === VALIDATION ===

# Check workspace compilation without building
[doc("Check compilation (no build)")]
check:
    @echo "=== Checking compilation ==="
    cargo check --workspace --all-features
    @echo "✓ Compilation check passed"
