#!/usr/bin/env zsh

export NIX_CONFIG="experimental-features = nix-command flakes"

echo "=== nix --version ==="
nix --version
echo

configs=(
  "nixos-local-wireguard-server"
  "nixos-blank"
  "nixos-songsheet"
  "nixos-database"
  "nixos-forgejo"
  "nixos-teamspeak"
  "nixos-traefik"
  "nixos-build-machine"
  "nixos-logs"
  "nixos-mail"
  "nixos-minecraft"
)

if [[ -n "$1" ]]; then
  configs=("$1")
fi

# Track results
passed_configs=()
failed_configs=()

for config in "${configs[@]}"; do
  echo "Dry-run: $config ==="
  if nix build --dry-run ".#nixosConfigurations.${config}.config.system.build.toplevel" 2>&1; then
    echo "[PASSED]: $config"
    passed_configs+=("$config")
  else
    echo "[FAILED]: $config"
    failed_configs+=("$config")
  fi
  echo
done

# Print summary
echo "BUILD SUMMARY"
echo "Total: ${#configs[@]} | Passed: ${#passed_configs[@]} | Failed: ${#failed_configs[@]}"
echo

if [[ ${#passed_configs[@]} -gt 0 ]]; then
  echo "Passed configs:"
  for c in "${passed_configs[@]}"; do
    echo "   - $c"
  done
  echo
fi

if [[ ${#failed_configs[@]} -gt 0 ]]; then
  echo "Failed configs:"
  for c in "${failed_configs[@]}"; do
    echo "   - $c"
  done
  echo
  exit 1
fi

echo "All configurations passed!"
exit 0
