#!/bin/bash
# quick-deploy.sh
# Quick deployment script that runs all deployment steps in sequence

set -euo pipefail

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

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

log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

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

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

info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

step() {
    echo ""
    echo "═══════════════════════════════════════════════════════════════"
    info "Step $1: $2"
    echo "═══════════════════════════════════════════════════════════════"
    echo ""
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║          Proxmox Quick Deployment                            ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    info "This script will guide you through the complete deployment process"
    echo ""
    
    # Step 1: Test Connectivity
    step "1" "Test Proxmox Connectivity"
    read -p "Test connectivity to Proxmox instances? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        "${SCRIPT_DIR}/test-proxmox-connectivity.sh"
    fi
    
    # Step 2: Setup DNS
    step "2" "Configure DNS Records"
    read -p "Setup DNS records via Cloudflare? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        if [ -z "${CLOUDFLARE_ZONE_ID:-}" ] || [ -z "${CLOUDFLARE_API_TOKEN:-}" ]; then
            warn "CLOUDFLARE_ZONE_ID and CLOUDFLARE_API_TOKEN must be set"
            read -p "Set them now? (y/N): " -n 1 -r
            echo
            if [[ $REPLY =~ ^[Yy]$ ]]; then
                read -p "Cloudflare Zone ID: " ZONE_ID
                read -sp "Cloudflare API Token: " API_TOKEN
                echo
                export CLOUDFLARE_ZONE_ID="$ZONE_ID"
                export CLOUDFLARE_API_TOKEN="$API_TOKEN"
            fi
        fi
        "${SCRIPT_DIR}/setup-dns-records.sh"
    fi
    
    # Step 3: Build Provider
    step "3" "Build Crossplane Provider"
    read -p "Build provider? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        "${SCRIPT_DIR}/deploy-crossplane-provider.sh"
    fi
    
    # Step 4: Create Secret
    step "4" "Create Proxmox Credentials Secret"
    read -p "Create Kubernetes secret? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        "${SCRIPT_DIR}/create-proxmox-secret.sh"
    fi
    
    # Step 5: Apply ProviderConfig
    step "5" "Apply ProviderConfig"
    read -p "Apply ProviderConfig? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        kubectl apply -f crossplane-provider-proxmox/examples/provider-config.yaml
        log "ProviderConfig applied"
    fi
    
    # Step 6: Verify Deployment
    step "6" "Verify Provider Deployment"
    "${SCRIPT_DIR}/verify-provider-deployment.sh"
    
    # Step 7: Deploy Test VMs
    step "7" "Deploy Test VMs"
    read -p "Deploy test VMs? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        "${SCRIPT_DIR}/deploy-test-vms.sh"
    fi
    
    # Step 8: Setup Monitoring
    step "8" "Setup Monitoring"
    read -p "Setup monitoring? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        "${SCRIPT_DIR}/setup-monitoring.sh"
    fi
    
    echo ""
    log "Quick deployment complete!"
    echo ""
    info "Summary:"
    info "  • Connectivity: Tested"
    info "  • DNS: Configured"
    info "  • Provider: Deployed"
    info "  • Credentials: Created"
    info "  • Test VMs: Deployed"
    info "  • Monitoring: Configured"
    echo ""
    info "Next: Review logs and verify all components are working"
}

main "$@"

