#!/bin/bash

set -e
set -o pipefail

# Parse arguments

SKIP_SCAFFOLD=false
SKIP_CLUSTER=false
SKIP_SERVICES=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-scaffold)
            SKIP_SCAFFOLD=true
            shift
            ;;
        --skip-docs)
            SKIP_DOCS=true
            shift
            ;;
        --skip-cluster)
            SKIP_CLUSTER=true
            shift
            ;;
        --skip-services)
            SKIP_SERVICES=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 [component-options]"
            echo ""
            echo "Complete Wild Cloud setup - runs all components in sequence."
            echo ""
            echo "Component Control Options:"
            echo "  --skip-scaffold     Skip scaffold setup (cloud initialization)"
            echo "  --skip-docs         Skip cloud documentation setup"
            echo "  --skip-cluster      Skip cluster setup"
            echo "  --skip-services     Skip services setup"
            echo "  -h, --help          Show this help message"
            echo ""
            echo "This script runs:"
            echo "  1. wild-setup-scaffold   # Cloud initialization and basic config"
            echo "  2. wild-setup-cluster    # Cluster infrastructure"
            echo "  3. wild-setup-services   # Cluster services"
            echo ""
            echo "You can also run these components individually:"
            echo "  - wild-setup-scaffold  [--update]"
            echo "  - wild-setup-cluster   [--skip-installer] [--skip-hardware] [--skip-configs]"
            echo "  - wild-setup-services  [--skip-install]"
            echo ""
            echo "For detailed options for each component, use:"
            echo "  wild-setup-scaffold --help"
            echo "  wild-setup-cluster --help"
            echo "  wild-setup-services --help"
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            echo "Usage: $0 [component-options]"
            echo "Use --help for full usage information"
            exit 1
            ;;
        *)
            echo "Unexpected argument: $1"
            echo "Usage: $0 [component-options]"
            echo "Use --help for full usage information"
            exit 1
            ;;
    esac
done

# Initialize Wild Cloud environment
if [ -z "${WC_ROOT}" ]; then
    print "WC_ROOT is not set."
    exit 1
else
    source "${WC_ROOT}/scripts/common.sh"
    init_wild_env
fi

print_header "Wild Cloud Setup"
print_info "Running complete Wild Cloud setup."
echo ""

# =============================================================================
# WC_HOME SCAFFOLDING
# =============================================================================

if [ "${SKIP_SCAFFOLD}" = false ]; then
    print_header "Cloud Home Setup"
    print_info "Scaffolding your cloud home..."
    
    if wild-setup-scaffold; then
        print_success "Cloud home setup completed"
    else
        print_error "Cloud home setup failed"
        exit 1
    fi
    echo ""
else
    print_info "Skipping Home Setup"
fi

# =============================================================================
# DOCS
# =============================================================================

if [ "${SKIP_DOCS}" = false ]; then
    print_header "Cloud Docs"
    print_info "Preparing your docs..."

    if wild-setup-docs; then
        print_success "Cloud docs setup completed"
    else
        print_error "Cloud docs setup failed"
        exit 1
    fi
    echo ""
else
    print_info "Skipping Docs Setup"
fi

# =============================================================================
# CLUSTER SETUP
# =============================================================================

if [ "${SKIP_CLUSTER}" = false ]; then
    print_header "Cluster Setup"
    print_info "Running wild-setup-cluster..."
    
    if wild-setup-cluster; then
        print_success "Cluster setup completed"
    else
        print_error "Cluster setup failed"
        exit 1
    fi
    echo ""
else
    print_info "Skipping Cluster Setup"
fi

# =============================================================================
# SERVICES SETUP
# =============================================================================

if [ "${SKIP_SERVICES}" = false ]; then
    print_header "Services Setup"
    print_info "Running wild-setup-services..."
    
    if wild-setup-services; then
        print_success "Services setup completed"
    else
        print_error "Services setup failed"
        exit 1
    fi
    echo ""
else
    print_info "Skipping cluster services setup"
fi

# =============================================================================
# FINAL SUMMARY
# =============================================================================

print_header "Wild Cloud Setup Finished!"

echo ""
if [ "${SKIP_SERVICES}" = false ] && command -v kubectl >/dev/null 2>&1; then
    if [ -f "${WC_HOME}/config.yaml" ]; then
        INTERNAL_DOMAIN=$(wild-config cloud.internalDomain 2>/dev/null || echo "your-internal-domain")
        print_info "Your Wild Cloud is ready!"
        echo "  Dashboard: https://dashboard.${INTERNAL_DOMAIN}"
        echo "  Get token: ./bin/dashboard-token"
    fi
else
    print_info "Complete the remaining setup steps to finalize your Wild Cloud deployment"
fi

print_success "Wild Cloud setup completed!"
