#!/bin/bash

# HGZero Backend Services Secrets Creation Script
# This script helps create Kubernetes secrets for the backend services

set -e

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
NAMESPACE="hgzero"

echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN}HGZero Secrets Creation${NC}"
echo -e "${GREEN}======================================${NC}"

# Check if kubectl is installed
if ! command -v kubectl &> /dev/null; then
    echo -e "${RED}Error: kubectl is not installed${NC}"
    exit 1
fi

# Verify connection to cluster
echo -e "${YELLOW}Verifying connection to Kubernetes cluster...${NC}"
if ! kubectl cluster-info &> /dev/null; then
    echo -e "${RED}Error: Cannot connect to Kubernetes cluster${NC}"
    exit 1
fi

# Check if namespace exists
if ! kubectl get namespace ${NAMESPACE} &> /dev/null; then
    echo -e "${RED}Error: Namespace '${NAMESPACE}' does not exist${NC}"
    echo -e "${YELLOW}Please run deploy.sh first to create the namespace${NC}"
    exit 1
fi

# Function to prompt for secret value
prompt_secret() {
    local prompt_text=$1
    local secret_value
    echo -n -e "${YELLOW}${prompt_text}: ${NC}"
    read -s secret_value
    echo ""
    echo -n "$secret_value"
}

# Create Database Secret
echo -e "${GREEN}Creating Database Secret...${NC}"
DB_HOST=$(prompt_secret "Enter Database Host")
DB_USERNAME=$(prompt_secret "Enter Database Username")
DB_PASSWORD=$(prompt_secret "Enter Database Password")

kubectl create secret generic db-secret \
    --from-literal=host="${DB_HOST}" \
    --from-literal=username="${DB_USERNAME}" \
    --from-literal=password="${DB_PASSWORD}" \
    --namespace=${NAMESPACE} \
    --dry-run=client -o yaml | kubectl apply -f -

echo -e "${GREEN}✓ Database secret created${NC}"
echo ""

# Create Azure Secret
echo -e "${GREEN}Creating Azure Secret...${NC}"
EVENTHUB_CONN=$(prompt_secret "Enter EventHub Connection String")
BLOB_CONN=$(prompt_secret "Enter Blob Storage Connection String")

kubectl create secret generic azure-secret \
    --from-literal=eventhub-connection-string="${EVENTHUB_CONN}" \
    --from-literal=blob-connection-string="${BLOB_CONN}" \
    --namespace=${NAMESPACE} \
    --dry-run=client -o yaml | kubectl apply -f -

echo -e "${GREEN}✓ Azure secret created${NC}"
echo ""

# Create Mail Secret
echo -e "${GREEN}Creating Mail Secret...${NC}"
MAIL_USERNAME=$(prompt_secret "Enter Mail Username")
MAIL_PASSWORD=$(prompt_secret "Enter Mail Password")

kubectl create secret generic mail-secret \
    --from-literal=username="${MAIL_USERNAME}" \
    --from-literal=password="${MAIL_PASSWORD}" \
    --namespace=${NAMESPACE} \
    --dry-run=client -o yaml | kubectl apply -f -

echo -e "${GREEN}✓ Mail secret created${NC}"
echo ""

# Verify secrets
echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN}Secrets Created Successfully${NC}"
echo -e "${GREEN}======================================${NC}"
kubectl get secrets -n ${NAMESPACE}

echo ""
echo -e "${YELLOW}Note: Secrets are stored in Kubernetes and can be viewed with:${NC}"
echo -e "  kubectl get secret <secret-name> -n ${NAMESPACE} -o yaml"
