# homelab-docs-deployment.nix - Deployment documentation generator CLI
{
  writeShellScriptBin,
  jq,
}:
writeShellScriptBin "homelab-docs-deployment" ''
  #!/usr/bin/env bash
  set -euo pipefail

  cat << EOF
  # Current Deployment State

  > Current homelab deployment configuration
  >
  > Generated on: $(date -R)
  > Working directory: $(pwd)

  ## Deployment Summary

  EOF

  # Get deployment summary
  deployment_summary=$(colmena eval -E '{ nodes, pkgs, lib, ... }:
    let
      homelabNodes = lib.filterAttrs (name: node: node.config.homelab.enable or false) nodes;
      allServices = lib.flatten (lib.mapAttrsToList (nodeName: node:
        lib.attrNames (lib.filterAttrs (n: v: v.enable or false) (node.config.homelab.services or {}))
      ) homelabNodes);
    in {
      totalNodes = lib.length (lib.attrNames nodes);
      homelabEnabledNodes = lib.length (lib.attrNames homelabNodes);
      uniqueServices = lib.length (lib.unique allServices);
      totalServiceInstances = lib.length allServices;
      nodeNames = lib.attrNames nodes;
      homelabNodeNames = lib.attrNames homelabNodes;
    }')

  total_nodes=$(echo "$deployment_summary" | ${jq}/bin/jq -r '.totalNodes')
  homelab_nodes=$(echo "$deployment_summary" | ${jq}/bin/jq -r '.homelabEnabledNodes')
  unique_services=$(echo "$deployment_summary" | ${jq}/bin/jq -r '.uniqueServices')
  service_instances=$(echo "$deployment_summary" | ${jq}/bin/jq -r '.totalServiceInstances')

  echo "| Metric | Count |"
  echo "|--------|-------|"
  echo "| Total Nodes | $total_nodes |"
  echo "| Homelab-Enabled Nodes | $homelab_nodes |"
  echo "| Unique Services | $unique_services |"
  echo "| Service Instances | $service_instances |"
  echo

  echo "## Node Status"
  echo

  # Get detailed node status
  node_status=$(colmena eval -E '{ nodes, pkgs, lib, ... }:
    lib.mapAttrs (name: node: {
      homelabEnabled = node.config.homelab.enable or false;
      environment = node.config.homelab.environment or "unknown";
      serviceCount = if (node.config.homelab.enable or false) then
        lib.length (lib.attrNames (lib.filterAttrs (n: v: v.enable or false) (node.config.homelab.services or {})))
      else 0;
      monitoringEnabled = if (node.config.homelab.enable or false) then
        node.config.homelab.monitoring.enable or false
      else false;
      backupsEnabled = if (node.config.homelab.enable or false) then
        node.config.homelab.backups.enable or false
      else false;
      proxyEnabled = if (node.config.homelab.enable or false) then
        node.config.homelab.reverseProxy.enable or false
      else false;
    }) nodes')

  echo "| Node | Homelab | Environment | Services | Monitoring | Backups | Proxy |"
  echo "|------|---------|-------------|----------|------------|---------|-------|"

  echo "$node_status" | ${jq}/bin/jq -r 'to_entries[] |
    [.key, (.value.homelabEnabled | if . then "✅" else "❌" end), .value.environment, (.value.serviceCount | tostring),
     (.value.monitoringEnabled | if . then "✅" else "❌" end),
     (.value.backupsEnabled | if . then "✅" else "❌" end),
     (.value.proxyEnabled | if . then "✅" else "❌" end)] |
    @tsv' | while IFS=$'\t' read -r node homelab env services monitoring backups proxy; do
      echo "| \`$node\` | $homelab | $env | $services | $monitoring | $backups | $proxy |"
  done

  echo
  echo "---"
  echo
  echo "*Deployment state extracted from live colmena configuration*"
''
