set fallback := true

export ENV_FILE := ".env.local"
export K8S_CONTEXT := env("K8S_CONTEXT", "")
export K8S_MASTER_NODE_NAME := env("K8S_MASTER_NODE_NAME", "")
export SERVER_IP := env("SERVER_IP", "")
export AGENT_IP := env("AGENT_IP", "")

check:
    #!/bin/bash
    set -euo pipefail
    if [ -z "${K8S_CONTEXT}" ]; then
        echo "K8S_CONTEXT is not set. Please execute 'just env::setup'" >&2
        exit 1
    fi    
    if [ -z "${K8S_MASTER_NODE_NAME}" ]; then
        echo "K8S_MASTER_NODE_NAME is not set. Please execute 'just env::setup'" >&2
        exit 1
    fi
    if [ -z "${SERVER_IP}" ]; then
        echo "SERVER_IP is not set. Please execute 'just env::setup'" >&2
        exit 1
    fi
    if [ -z "${AGENT_IP}" ]; then
        echo "AGENT_IP is not set. Please execute 'just env::setup'" >&2
        exit 1
    fi

setup:
    #!/bin/bash
    set -euo pipefail
    if [ -f ../.env.local ]; then
        echo ".env.local already exists." >&2
        if gum confirm "Do you want to overwrite it?"; then
            K8S_CONTEXT=""
            SERVER_IP=""
            AGENT_IP=""
        elif [[ $? -eq 130 ]]; then
            echo "Setup cancelled by user." >&2
            exit 1
        else
            echo "Aborting setup." >&2
            exit 1
        fi
    fi
    while [ -z "${K8S_CONTEXT}" ]; do
        if ! K8S_CONTEXT=$(
            gum input --prompt="Context name: " \
            --width=100 --placeholder="context"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done
    while [ -z "${K8S_MASTER_NODE_NAME}" ]; do
        if ! K8S_MASTER_NODE_NAME=$(
            gum input --prompt="Master Node Hostname: " \
            --width=100 --placeholder="Master Node Name"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done
    while [ -z "${SERVER_IP}" ]; do
        if ! SERVER_IP=$(
            gum input --prompt="IP of Server/Master Node: " \
            --width=100 --placeholder="Master Node IP"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done
    while [ -z "${AGENT_IP}" ]; do
        if ! AGENT_IP=$(
            gum input --prompt="IP of Agent Node: " \
            --width=100 --placeholder="Agent Node IP"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done

    while [ -z "${METALLB_ADDRESS_RANGE}" ]; do
        if ! METALLB_ADDRESS_RANGE=$(
            gum input --prompt="IP Range for LoadBalancer: " \
            --width=100 --placeholder="[x.x.x.x-y.y.y.y]"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done

    while [ -z "${CLOUDFLARE_API_TOKEN}" ]; do
        if ! CLOUDFLARE_API_TOKEN=$(
            gum input --prompt="Cloudflare API Token: " \
            --width=100 --placeholder="API Token" --password
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done

    while [ -z "${ACME_EMAIL}" ]; do
        if ! ACME_EMAIL=$(
            gum input --prompt="ACME Email for Cert-Manager: " \
            --width=100 --placeholder="Email"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done

    while [ -z "${EXTERNAL_DOMAIN}" ]; do
        if ! EXTERNAL_DOMAIN=$(
            gum input --prompt="External Domain: " \
            --width=100 --placeholder="Domain"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done
    
    while [ -z "${VAULT_HOST}" ]; do
        if ! VAULT_HOST=$(
            gum input --prompt="Vault hostname: " \
            --width=100 --placeholder="vault"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done
    while [ -z "${AUTHENTIK_HOST}" ]; do
        if ! AUTHENTIK_HOST=$(
            gum input --prompt="Authentik hostname: " \
            --width=100 --placeholder="authentik"
        ); then
            echo "Setup cancelled." >&2
            exit 1
        fi
    done

    echo "Generating .env.local file..."    
    rm -f ../.env.local
    gomplate -f env.local.gomplate -o ../.env.local
