set fallback := true

export K8S_CONTEXT := env("K8S_CONTEXT", "")
export K8S_MASTER_NODE_NAME := env("K8S_MASTER_NODE_NAME", "")
export EXTERNAL_K8S_HOST := env("EXTERNAL_K8S_HOST", "")
export KEYCLOAK_HOST := env("KEYCLOAK_HOST", "")
export KEYCLOAK_REALM := env("KEYCLOAK_REALM", "buunstack")
export K8S_OIDC_CLIENT_ID := env('K8S_OIDC_CLIENT_ID', "k8s")
export K3S_ENABLE_REGISTRY := env("K3S_ENABLE_REGISTRY", "true")
export SERVER_IP := env("K3S_SERVER_IP","192.168.178.45")
export AGENT_IP := env("K3S_AGENT_IP","192.168.178.75")
export USER := env("K3S_USER","basti")
export LONGHORN_NAMESPACE := env("LONGHORN_NAMESPACE","longhorn-system")

[private]
default:
    @just --list --unsorted --list-submodules

install:
    #!/bin/bash
    set -euo pipefail
    just env::check
    username=$(gum input --prompt="SSH username: " --value="${USER}" --width=100)
    kubeconfig=""
    context=""
    if gum confirm "Update KUBECONFIG?"; then
        kubeconfig=$(
            gum input --prompt="KUBECONFIG file: " --value="${HOME}/.kube/config" --width=100
        )
        context=$(
            gum input --prompt="Context name: " --value="${K8S_CONTEXT}" --width=100
        )
    fi

    args=(
        "install"
        "--context" "${context}"
        "--host" "${K8S_MASTER_NODE_NAME}"
        "--user" "${username}"
        "--no-extras" # 
    )

    if [ -n "${kubeconfig}" ]; then
        mkdir -p "$(dirname "${kubeconfig}")"
        args+=("--local-path" "${kubeconfig}" "--merge")
    fi
    echo "Running: k3sup ${args[@]}"
    k3sup "${args[@]}"

    if [ -n "${context}" ]; then
        kubectl config use-context "${context}"
    fi

    if [ "${K3S_ENABLE_REGISTRY}" = "true" ]; then
        echo "Setting up local Docker registry..."

        # Deploy Docker registry to cluster
        kubectl apply -f ./registry/registry.yaml

        # Set Pod Security Standard for registry namespace
        kubectl label namespace registry pod-security.kubernetes.io/enforce=restricted --overwrite

        # Wait for registry deployment
        echo "Waiting for registry to be ready..."
        kubectl wait --for=condition=available --timeout=60s deployment/registry -n registry

        # Configure registries.yaml for k3s
        just configure-registry

        echo "✓ Local Docker registry deployed and configured"
        echo ""
        echo "Registry accessible at:"
        echo "  localhost:30500"
        echo ""
        echo "Usage:"
        echo "  export DOCKER_HOST=ssh://${K8S_MASTER_NODE_NAME}"
        echo "  docker build -t localhost:30500/myapp:latest ."
        echo "  docker push localhost:30500/myapp:latest"
        echo "  kubectl run myapp --image=localhost:30500/myapp:latest"
    fi

    echo "k3s cluster installed on ${K8S_MASTER_NODE_NAME}."


uninstall:
    #!/bin/bash
    set -euo pipefail
    if gum confirm "Uninstall k3s from ${K8S_MASTER_NODE_NAME}?"; then

        if gum confirm "Also remove Agent node at ${AGENT_IP}?"; then
            echo "Removing Agent node at ${AGENT_IP}..."
            ssh "${AGENT_IP}" "/usr/local/bin/k3s-agent-uninstall.sh"
        fi

        echo "Removing content of Server node..."
        ssh "${K8S_MASTER_NODE_NAME}" "/usr/local/bin/k3s-uninstall.sh"
        echo "Cleaning up kubeconfig entries..."
        cluster_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${K8S_CONTEXT}\") | .context.cluster // empty")
        user_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${K8S_CONTEXT}\") | .context.user // empty")
        if kubectl config get-contexts "${K8S_CONTEXT}" &>/dev/null; then
            kubectl config delete-context "${K8S_CONTEXT}"
            echo "Deleted context: ${K8S_CONTEXT}"
        fi
        if [ -n "${cluster_name}" ] && kubectl config get-clusters | grep -q "^${cluster_name}$"; then
            kubectl config delete-cluster "${cluster_name}"
            echo "Deleted cluster: ${cluster_name}"
        fi
        if [ -n "${user_name}" ] && kubectl config get-users | grep -q "^${user_name}$"; then
            kubectl config delete-user "${user_name}"
            echo "Deleted user: ${user_name}"
        fi
        echo "k3s cluster uninstalled from ${K8S_CONTEXT}."
    else
        echo "Uninstallation cancelled." >&2
        exit 1
    fi

add-agent:
    #!/bin/bash
    set -euo pipefail
    just env::check

    username=$(gum input --prompt="SSH username: " --value="${USER}" --width=100)
    new_agent_ip=$(gum input --prompt="Agent IP to join cluster: " --value="${AGENT_IP}" --width=100)

    args=(
        "join"
        "--ip" "${new_agent_ip}"
        "--server-ip" "${SERVER_IP}"
        "--user" "${username}"
    )


    echo "Running: k3sup ${args[*]}"
    k3sup "${args[@]}"
    echo "Agent node at ${new_agent_ip} added to cluster."
 
# Configure k3s to use local registry
configure-registry:
    #!/bin/bash
    set -euo pipefail
    echo "Configuring k3s registries.yaml..."

    ssh "${K8S_MASTER_NODE_NAME}" "sudo mkdir -p /etc/rancher/k3s"
    gomplate -f ./registry/registries.gomplate.yaml | ssh "${K8S_MASTER_NODE_NAME}" "sudo tee /etc/rancher/k3s/registries.yaml > /dev/null"

    echo "Restarting k3s to apply registry configuration..."
    ssh "${K8S_MASTER_NODE_NAME}" "sudo systemctl restart k3s"
    echo "✓ Registry configuration applied"


stop:
    #!/bin/bash
    set -euo pipefail
    START_TIME=$(date +%s)
    elapsed() {
        echo "$(($(date +%s) - START_TIME))s"
    }
    nodenames=$(kubectl get nodes -o=jsonpath="{.items[*]['metadata.name']}")
    for node in ${nodenames}; do
        kubectl drain "${node}" --ignore-daemonsets --delete-emptydir-data --force --disable-eviction --grace-period=60 --timeout=180s 2>&1 || true
        kubectl cordon "${node}"
        echo "Node ${node} stopped."
    done
    echo "Drain complete. Nodes are cordoned and drained."

    if helm status longhorn -n ${LONGHORN_NAMESPACE} &>/dev/null; then
        echo "[$(elapsed)] Waiting for Longhorn volumes to be detached..."
        TIMEOUT=90
        ELAPSED=0
        while [ $ELAPSED -lt $TIMEOUT ]; do

            ATTACHED=$(kubectl get volumes.longhorn.io -n ${LONGHORN_NAMESPACE} -o json 2>/dev/null | \
                jq -r '.items[] | select(.status.state == "attached") | .metadata.name' 2>/dev/null || true)

            if [ -z "$ATTACHED" ]; then
                echo "[$(elapsed)] ✓ All Longhorn volumes detached successfully"
                break
            fi

            ATTACHED_COUNT=$(echo "$ATTACHED" | grep -c . || echo 0)
            echo "  Still waiting for $ATTACHED_COUNT volume(s) to detach..."
            sleep 2
            ELAPSED=$((ELAPSED + 2))
        done
        if [ $ELAPSED -ge $TIMEOUT ]; then
            echo "[$(elapsed)] ⚠ Warning: Timeout waiting for volumes to detach"
        fi
    fi

    for node in ${nodenames}; do
        echo "[$(elapsed)] Stopping and disabling k3s service..."
        ssh "${node}" "sudo systemctl stop k3s 2>/dev/null || true"
        ssh "${node}" "sudo systemctl disable k3s 2>/dev/null || true"
    done

start:
    #!/bin/bash
    set -euo pipefail
    
    is_schedulable() {
        node_name="$1"
        ! kubectl get node "$node_name" -o jsonpath='{.spec.unschedulable}' 2>/dev/null | grep -q "true"
    }
    
    nodenames=$(kubectl get nodes -o=jsonpath="{.items[*]['metadata.name']}")


    for node in ${nodenames}; do
        echo "Starting k3s service on ${node}..."

        if is_schedulable "$node"; then
            echo "✓ Node $node is already schedulable"
            exit 0
        fi

        echo "Uncordoning node $node..."
        kubectl uncordon "$node" 2>&1 || true

        echo "Wait for every node to become Ready..."
    done