#!/bin/bash
# Create Kubernetes secret from local Claude credentials
# Run this after authenticating with `claude` locally

set -e

# Ensure kubeconfig is set
if [[ -z "$KUBECONFIG" ]]; then
    echo "Error: KUBECONFIG not set"
    echo "Run: export KUBECONFIG=~/.kube/orchard9-k3sf.yaml"
    exit 1
fi

# Check if Claude credentials exist
CLAUDE_DIR="$HOME/.claude"
if [[ ! -d "$CLAUDE_DIR" ]]; then
    echo "Error: Claude credentials not found at $CLAUDE_DIR"
    echo "Run 'claude' first to authenticate"
    exit 1
fi

# Create namespace if it doesn't exist
kubectl create namespace rdev --dry-run=client -o yaml | kubectl apply -f -

# Create secret from the .claude directory
echo "Creating claude-credentials secret from $CLAUDE_DIR..."

# We need to create the secret with all files in ~/.claude
kubectl create secret generic claude-credentials \
    --namespace=rdev \
    --from-file="$CLAUDE_DIR" \
    --dry-run=client -o yaml | kubectl apply -f -

echo "Secret created successfully!"
echo ""
echo "Verify with:"
echo "  kubectl get secret claude-credentials -n rdev"
