#!/bin/bash
# Veza V5 Ultra Deployment Demo Script
# Shows the deployment process and configuration

set -euo pipefail

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

# Functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

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

show_header() {
    echo
    echo "========================================"
    echo "Veza V5 Ultra Deployment Demo"
    echo "========================================"
    echo
}

check_system() {
    log_info "Checking system information..."
    
    echo "System: $(uname -a)"
    echo "Python: $(python3 --version 2>/dev/null || echo 'Not available')"
    echo "User: $(whoami)"
    echo "Home: $HOME"
    echo
}

check_packages() {
    log_info "Checking required packages..."
    
    local packages=("python3" "curl" "git" "wget" "ansible")
    for pkg in "${packages[@]}"; do
        if command -v "$pkg" &> /dev/null; then
            log_success "$pkg: Available"
        else
            log_warning "$pkg: Not installed"
        fi
    done
    echo
}

check_ansible() {
    log_info "Checking Ansible setup..."
    
    echo "Ansible version: $(ansible --version | head -1)"
    echo "Ansible collections:"
    ansible-galaxy collection list 2>/dev/null | grep -E "(community|incus)" || echo "  No relevant collections found"
    echo
}

check_network() {
    log_info "Checking network configuration..."
    
    echo "Network interfaces:"
    ip addr show | grep -E "(inet |UP)" | head -10
    echo
    
    echo "Default route:"
    ip route show | grep default
    echo
}

check_target_host() {
    log_info "Checking target host connectivity..."
    
    local target_host="192.168.0.12"
    if ping -c 1 -W 1 "$target_host" &> /dev/null; then
        log_success "Target host $target_host is reachable"
    else
        log_warning "Target host $target_host is not reachable"
        echo "  This is expected if the host is not currently running"
    fi
    echo
}

show_deployment_steps() {
    log_info "Veza V5 Ultra Deployment Steps:"
    echo
    echo "1. Bootstrap Host (00-bootstrap-remote.yml)"
    echo "   - Install Python, sudo, curl, gnupg, net-tools"
    echo "   - Configure SSH and firewall"
    echo "   - Install Incus dependencies"
    echo
    echo "2. Install Incus + OVN (10-incus-ovn.yml)"
    echo "   - Install Incus via snap"
    echo "   - Install OVN packages"
    echo "   - Create OVN network 'veza-ovn'"
    echo
    echo "3. Create Containers (20-incus-containers.yml)"
    echo "   - veza-haproxy (Debian 12) - Edge proxy"
    echo "   - veza-backend (Debian 12) - Go API on 8080"
    echo "   - veza-chat (Debian 12) - Rust WebSocket on 8081"
    echo "   - veza-stream (Debian 12) - Rust HLS on 8082"
    echo "   - veza-web (Debian 12) - React + nginx on 80"
    echo
    echo "4. Configure HAProxy + ACME (30-haproxy-in-container.yml)"
    echo "   - Install HAProxy in container"
    echo "   - Setup Let's Encrypt HTTP-01 validation"
    echo "   - Configure routing and SSL termination"
    echo "   - Generate certificates for veza.talas.fr"
    echo
    echo "5. Deploy Applications (40-veza-apps.yml)"
    echo "   - Build and run Go backend with systemd"
    echo "   - Build and run Rust chat server with systemd"
    echo "   - Build and run Rust stream server with systemd"
    echo "   - Build React app and serve with nginx"
    echo
    echo "6. Run Smoke Tests (50-smoke.yml)"
    echo "   - Test HTTPS access"
    echo "   - Test API endpoints"
    echo "   - Test WebSocket connectivity"
    echo "   - Test HLS streaming"
    echo
}

show_architecture() {
    log_info "Veza V5 Ultra Architecture:"
    echo
    echo "┌─────────────────────────────────────────────────────────────┐"
    echo "│                    Internet (veza.talas.fr)                 │"
    echo "└─────────────────────┬───────────────────────────────────────┘"
    echo "                      │"
    echo "┌─────────────────────▼───────────────────────────────────────┐"
    echo "│                HAProxy Container (80/443)                   │"
    echo "│              - SSL Termination                              │"
    echo "│              - Let's Encrypt ACME                          │"
    echo "│              - Request Routing                             │"
    echo "└─────────────────────┬───────────────────────────────────────┘"
    echo "                      │"
    echo "┌─────────────────────▼───────────────────────────────────────┐"
    echo "│                    OVN Network                              │"
    echo "│                  (veza-ovn)                                 │"
    echo "└─────┬─────────┬─────────┬─────────┬─────────────────────────┘"
    echo "      │         │         │         │"
    echo "┌─────▼───┐ ┌───▼───┐ ┌───▼───┐ ┌───▼───┐"
    echo "│ Backend │ │ Chat  │ │Stream │ │ Web   │"
    echo "│  :8080  │ │ :8081 │ │ :8082 │ │  :80  │"
    echo "│  (Go)   │ │(Rust) │ │(Rust) │ │(React)│"
    echo "└─────────┘ └───────┘ └───────┘ └───────┘"
    echo
}

show_commands() {
    log_info "Deployment Commands:"
    echo
    echo "# Full deployment:"
    echo "./deploy-veza.sh"
    echo
    echo "# Step-by-step deployment:"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/00-bootstrap-remote.yml"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/10-incus-ovn.yml"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/20-incus-containers.yml"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/30-haproxy-in-container.yml -e domain=veza.talas.fr -e acme_email=ops@talas.fr"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/40-veza-apps.yml"
    echo "ansible-playbook -i inventory/prod/hosts.yml playbooks/50-smoke.yml"
    echo
    echo "# Custom domain:"
    echo "./deploy-veza.sh -d myapp.example.com -e admin@example.com"
    echo
}

show_next_steps() {
    log_info "Next Steps:"
    echo
    echo "1. Ensure target host (192.168.0.12) is running and accessible"
    echo "2. Verify SSH key authentication works:"
    echo "   ssh senke@192.168.0.12 'echo \"SSH test successful\"'"
    echo "3. Run the deployment:"
    echo "   ./deploy-veza.sh"
    echo "4. Point DNS A record for veza.talas.fr to 192.168.0.12"
    echo "5. Re-run HAProxy playbook to get Let's Encrypt certificate"
    echo
}

main() {
    show_header
    check_system
    check_packages
    check_ansible
    check_network
    check_target_host
    show_deployment_steps
    show_architecture
    show_commands
    show_next_steps
    
    log_success "Demo completed! Veza V5 Ultra deployment is ready to run."
    echo
}

main "$@"
