#!/usr/bin/env bash
# Complete automated setup - runs all automation steps

set -euo pipefail

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

# Colors
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}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

echo ""
log_info "=========================================="
log_info "  Complete Automated Cloudflare Setup"
log_info "=========================================="
echo ""

# Step 1: Verify prerequisites
log_info "Step 1: Verifying prerequisites..."
if ! "$SCRIPT_DIR/verify-prerequisites.sh"; then
    log_error "Prerequisites check failed"
    exit 1
fi

# Step 2: Run Cloudflare API automation
log_info ""
log_info "Step 2: Running Cloudflare API automation..."
log_info "This will create tunnels, DNS records, and Access applications"
echo ""

if ! "$SCRIPT_DIR/automate-cloudflare-setup.sh"; then
    log_error "Cloudflare API automation failed"
    exit 1
fi

# Step 3: Parse output and save credentials
log_info ""
log_info "Step 3: Saving tunnel credentials..."
log_warn "Note: You'll need to manually extract tunnel IDs and tokens from the output above"
log_warn "Then run: ./scripts/save-tunnel-credentials.sh <name> <id> <token>"
echo ""

# Step 4: Install systemd services
log_info ""
log_info "Step 4: Installing systemd services..."
if ! "$SCRIPT_DIR/setup-multi-tunnel.sh" --skip-credentials; then
    log_warn "Service installation may need manual completion"
fi

# Step 5: Start services
log_info ""
log_info "Step 5: Starting tunnel services..."
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
VMID="${VMID:-102}"

if command -v pct &> /dev/null; then
    for tunnel in ml110 r630-01 r630-02; do
        if pct exec "$VMID" -- systemctl start "cloudflared-${tunnel}.service" 2>/dev/null; then
            log_success "Started cloudflared-${tunnel}.service"
        else
            log_warn "Could not start cloudflared-${tunnel}.service (may need credentials first)"
        fi
    done
else
    log_warn "Cannot start services remotely. Run manually:"
    echo "  ssh root@${PROXMOX_HOST} 'pct exec $VMID -- systemctl start cloudflared-*'"
fi

# Step 6: Health check
log_info ""
log_info "Step 6: Running health check..."
if "$SCRIPT_DIR/check-tunnel-health.sh"; then
    log_success "Health check completed"
else
    log_warn "Health check found issues (may be expected if credentials not saved yet)"
fi

echo ""
log_success "=========================================="
log_success "  Automated Setup Complete"
log_success "=========================================="
echo ""
log_info "Next steps:"
echo ""
echo "1. Extract tunnel IDs and tokens from Step 2 output"
echo "2. Save credentials:"
echo "   ./scripts/save-tunnel-credentials.sh ml110 <id> <token>"
echo "   ./scripts/save-tunnel-credentials.sh r630-01 <id> <token>"
echo "   ./scripts/save-tunnel-credentials.sh r630-02 <id> <token>"
echo ""
echo "3. Start services:"
echo "   systemctl start cloudflared-*"
echo ""
echo "4. Verify:"
echo "   ./scripts/check-tunnel-health.sh"
echo ""

