{
  writeShellScriptBin,
  jq,
}:
writeShellScriptBin "homelab-docs-fleet" ''
  #!/usr/bin/env bash
  set -euo pipefail

  cat << EOF
  # Homelab Fleet Overview

  > Auto-generated fleet overview
  >
  > Generated on: $(date -R)
  > Source: $(pwd)

  ## Fleet Statistics

  EOF

  # Get basic fleet stats
  echo "### Basic Information"
  echo

  fleet_stats=$(colmena eval -E '{ nodes, pkgs, lib, ... }: {
    totalNodes = lib.length (lib.attrNames nodes);
    nodeNames = lib.attrNames nodes;
  }')

  total_nodes=$(echo "$fleet_stats" | ${jq}/bin/jq -r '.totalNodes')
  node_names=$(echo "$fleet_stats" | ${jq}/bin/jq -r '.nodeNames[]' | tr '\n' ' ')

  echo "| Metric | Value |"
  echo "|--------|-------|"
  echo "| Total Nodes | $total_nodes |"
  echo "| Node Names | $node_names |"
  echo

  # Get homelab-enabled nodes
  echo "### Homelab Configuration"
  echo

  homelab_info=$(colmena eval -E '{ nodes, pkgs, lib, ... }: {
    homelabNodes = lib.mapAttrs (name: node: {
      enabled = node.config.homelab.enable or false;
      hostname = node.config.homelab.hostname or null;
      environment = node.config.homelab.environment or null;
    }) nodes;
  }')

  echo "| Node | Homelab Enabled | Hostname | Environment |"
  echo "|------|----------------|----------|-------------|"

  echo "$homelab_info" | ${jq}/bin/jq -r '.homelabNodes | to_entries[] |
    [.key, (.value.enabled | tostring), (.value.hostname // "N/A"), (.value.environment // "N/A")] |
    @tsv' | while IFS=$'\t' read -r node enabled hostname environment; do
      enabled_icon=$(if [[ "$enabled" == "true" ]]; then echo "✅"; else echo "❌"; fi)
      echo "| \`$node\` | $enabled_icon | $hostname | $environment |"
  done

  echo

  # Get service distribution
  echo "### Service Distribution"
  echo

  service_info=$(colmena eval -E '{ nodes, pkgs, lib, ... }:
    lib.mapAttrs (name: node:
      if (node.config.homelab.enable or false) then {
        serviceCount = lib.length (lib.attrNames (lib.filterAttrs (n: v: v.enable or false) (node.config.homelab.services or {})));
        serviceNames = lib.attrNames (lib.filterAttrs (n: v: v.enable or false) (node.config.homelab.services or {}));
      } else {
        serviceCount = 0;
        serviceNames = [];
      }
    ) nodes')

  echo "| Node | Service Count | Services |"
  echo "|------|---------------|----------|"

  echo "$service_info" | ${jq}/bin/jq -r 'to_entries[] |
    [.key, (.value.serviceCount | tostring), (.value.serviceNames | join(", "))] |
    @tsv' | while IFS=$'\t' read -r node count services; do
      echo "| \`$node\` | $count | $services |"
  done

  echo
  echo "---"
  echo
  echo "*Fleet overview generated from colmena evaluation*"
''
