#!/usr/bin/env bash
# Quick setup script for SMOM-DBIS-138 Proxmox deployment

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

log_info "SMOM-DBIS-138 Proxmox Deployment Setup"
log_info "======================================"
log_info ""

# Check if running on Proxmox host
if ! command -v pct &>/dev/null; then
    log_error "This script must be run on a Proxmox host"
    log_error "The 'pct' command was not found"
    exit 1
fi

# Check root
if [[ $EUID -ne 0 ]]; then
    log_error "This script must be run as root"
    exit 1
fi

# Create config directory if it doesn't exist
mkdir -p "$SCRIPT_DIR/config"

# Setup proxmox.conf
if [[ ! -f "$SCRIPT_DIR/config/proxmox.conf" ]]; then
    log_info "Creating proxmox.conf from example..."
    cp "$SCRIPT_DIR/config/proxmox.conf.example" "$SCRIPT_DIR/config/proxmox.conf"
    log_success "Created proxmox.conf"
    log_warn "Please edit config/proxmox.conf with your Proxmox settings"
else
    log_info "proxmox.conf already exists, skipping..."
fi

# Setup network.conf
if [[ ! -f "$SCRIPT_DIR/config/network.conf" ]]; then
    log_info "Creating network.conf from example..."
    cp "$SCRIPT_DIR/config/network.conf.example" "$SCRIPT_DIR/config/network.conf"
    log_success "Created network.conf"
    log_warn "Please review config/network.conf and adjust network settings if needed"
else
    log_info "network.conf already exists, skipping..."
fi

# Check for OS template
log_info "Checking for Debian 12 OS template..."
if pveam list local | grep -q "debian-12-standard"; then
    log_success "Debian 12 template found"
else
    log_warn "Debian 12 template not found"
    log_info "Downloading Debian 12 template (this may take a while)..."
    if pveam download local debian-12-standard_12.2-1_amd64.tar.zst; then
        log_success "Template downloaded"
    else
        log_error "Failed to download template"
        log_info "You can download it manually from Proxmox UI or run:"
        log_info "  pveam download local debian-12-standard_12.2-1_amd64.tar.zst"
    fi
fi

# Make scripts executable
log_info "Setting script permissions..."
find "$SCRIPT_DIR/scripts" -name "*.sh" -exec chmod +x {} \;
find "$SCRIPT_DIR/install" -name "*.sh" -exec chmod +x {} \;
chmod +x "$SCRIPT_DIR/lib"/*.sh 2>/dev/null || true
log_success "Scripts are now executable"

# Create log directory
mkdir -p "$SCRIPT_DIR/logs"
log_success "Created logs directory"

log_info ""
log_success "Setup completed!"
log_info ""
log_info "Next steps:"
log_info "1. Edit config/proxmox.conf with your Proxmox connection details"
log_info "2. Review and adjust config/network.conf if needed"
log_info "3. Run deployment: ./scripts/deployment/deploy-all.sh"
log_info ""
log_info "For detailed instructions, see: docs/DEPLOYMENT.md"

