#!/bin/bash

set -e
set -o pipefail

# Parse arguments

SKIP_INSTALL=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-install)
            SKIP_INSTALL=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 [options]"
            echo ""
            echo "Install Kubernetes cluster services (Phase 4)."
            echo ""
            echo "Options:"
            echo "  --skip-install      Generate service configs but skip installation"
            echo "  -h, --help          Show this help message"
            echo ""
            echo "This script will:"
            echo "  - Generate cluster service configurations"
            echo "  - Install core services (MetalLB, Traefik, cert-manager, etc.)"
            echo "  - Each service will prompt for its required configuration"
            echo ""
            echo "Prerequisites:"
            echo "  - Run 'wild-setup-scaffold' to initialize the cloud"
            echo "  - Run 'wild-setup-cluster' to set up cluster infrastructure"
            echo "  - Kubernetes cluster must be running and kubectl configured"
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            echo "Usage: $0 [options]"
            echo "Use --help for full usage information"
            exit 1
            ;;
        *)
            echo "Unexpected argument: $1"
            echo "Usage: $0 [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

# Check cluster configuration
if [ -z "$(get_current_config "cluster.name")" ]; then
    print_error "Cluster configuration is missing"
    print_info "Run 'wild-setup-cluster' first to configure cluster settings"
    exit 1
fi

print_header "Wild Cloud Services Setup"
print_info "Installing Kubernetes cluster services"
echo ""

if ! command -v kubectl >/dev/null 2>&1; then
    print_error "kubectl is not installed or not in PATH"
    print_info "Please install kubectl and configure it to connect to your cluster"
    exit 1
fi

if ! kubectl cluster-info >/dev/null 2>&1; then
    print_error "kubectl is not configured to connect to your cluster"
    print_info "Please configure kubectl to connect to your Kubernetes cluster"
    exit 1
fi

# Generate cluster services setup files

wild-cluster-services-generate --force

# Apply cluster services to cluster

if [ "${SKIP_INSTALL}" = false ]; then
    wild-cluster-services-up
    SERVICES_INSTALLED=true
else
    print_info "Skipping cluster services installation (--skip-install specified)"
    print_info "You can install them later with: wild-cluster-services-up"
fi

# Summary output

print_header "Wild Cloud Services Setup Complete!"
echo ""

if [ "${SERVICES_INSTALLED:-false}" = true ]; then
    
    INTERNAL_DOMAIN=$(wild-config cloud.internalDomain 2>/dev/null || echo "your-internal-domain")
    echo "Next steps:"
    echo "  1. Access the dashboard at: https://dashboard.${INTERNAL_DOMAIN}"
    echo "  2. Get the dashboard token with: wild-dashboard-token"
    echo ""
    echo "To verify components, run:"
    echo "  - kubectl get pods -n cert-manager"
    echo "  - kubectl get pods -n externaldns"
    echo "  - kubectl get pods -n kubernetes-dashboard"
    echo "  - kubectl get clusterissuers"

else
    echo "Next steps:"
    echo "  1. Ensure your cluster is running and kubectl is configured"
    echo "  2. Install services with: wild-cluster-services-up"
    echo "  3. Verify components are running correctly"
fi

print_success "Wild Cloud setup completed!"
