# Helper script: scripts/deploy-homelab.sh
#!/bin/bash
set -euo pipefail

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== Homelab Deployment Script ===${NC}"

# Function to print colored output
log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if colmena is available
if ! command -v colmena &> /dev/null; then
    error "colmena is not installed. Please install it first."
    exit 1
fi

# Parse arguments
COMMAND=${1:-"deploy"}
TARGET=${2:-""}

case $COMMAND in
    "deploy")
        if [ -n "$TARGET" ]; then
            log "Deploying to specific target: $TARGET"
            colmena apply --on "$TARGET"
        else
            log "Deploying to all targets"
            colmena apply
        fi
        ;;
    "build")
        if [ -n "$TARGET" ]; then
            log "Building specific target: $TARGET"
            colmena build --on "$TARGET"
        else
            log "Building all targets"
            colmena build
        fi
        ;;
    "status")
        log "Checking deployment status"
        colmena apply --dry-run
        ;;
    "config")
        log "Showing global configuration summary"
        # Extract global configs from all nodes
        colmena eval ./scripts/config.nix | jq .
        ;;
    "backup-status")
        log "Checking backup status across all nodes"
        if [ -n "$TARGET" ]; then
            colmena exec --on "$TARGET" -- backup-status
        else
            colmena exec -- backup-status
        fi
        ;;
    "monitoring")
        log "Collecting monitoring endpoints"
        nix eval --json .#colmena --apply 'colmena:
          let
            lib = (import <nixpkgs> {}).lib;
            nodes = removeAttrs colmena ["meta"];
            collectEndpoints = lib.flatten (
              lib.mapAttrsToList (name: node:
                if node ? config.homelab.global.monitoring.endpoints then
                  map (e: {
                    node = name;
                    hostname = node.config.homelab.global.hostname;
                    endpoint = "${e.name}:${toString e.port}${e.path}";
                    job = e.jobName;
                  }) node.config.homelab.global.monitoring.endpoints
                else []
              ) nodes
            );
          in collectEndpoints
        ' | jq .
        ;;
    "help")
        echo "Usage: $0 [COMMAND] [TARGET]"
        echo ""
        echo "Commands:"
        echo "  deploy [TARGET]    Deploy to all nodes or specific target"
        echo "  build [TARGET]     Build configuration for all nodes or specific target"
        echo "  status            Show deployment status (dry-run)"
        echo "  config            Show global configuration summary"
        echo "  backup-status     Check backup status on all nodes"
        echo "  monitoring        List all monitoring endpoints"
        echo "  help              Show this help message"
        echo ""
        echo "Examples:"
        echo "  $0 deploy media-server    # Deploy only to media-server"
        echo "  $0 build                  # Build all configurations"
        echo "  $0 config                 # Show global config summary"
        ;;
    *)
        error "Unknown command: $COMMAND"
        echo "Run '$0 help' for usage information"
        exit 1
        ;;
esac
