#!/usr/bin/env bash
# Complete deployment script - runs all setup steps

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TUNNELS_DIR="$(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 ""
echo "=========================================="
echo "  Cloudflare Multi-Tunnel Deployment"
echo "=========================================="
echo ""

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

echo ""
log_info "Step 2: Running setup script..."
log_warn "You will be prompted for tunnel credentials"
echo ""

# Step 2: Run setup
if "$SCRIPT_DIR/setup-multi-tunnel.sh"; then
    log_success "Setup completed"
else
    log_error "Setup failed"
    exit 1
fi

echo ""
log_info "Step 3: 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 tunnels not fully configured)"
fi

echo ""
log_success "=========================================="
log_success "  Deployment Complete"
log_success "=========================================="
echo ""
log_info "Next steps:"
echo ""
echo "1. Verify tunnels are running:"
echo "   systemctl status cloudflared-*"
echo ""
echo "2. Configure Cloudflare Access:"
echo "   See: docs/CLOUDFLARE_ACCESS_SETUP.md"
echo ""
echo "3. Start monitoring:"
echo "   ./scripts/monitor-tunnels.sh --daemon"
echo ""
echo "4. Test access:"
echo "   curl -I https://ml110-01.d-bis.org"
echo ""

