#!/usr/bin/env bash
set -euo pipefail

if ! command -v openssl >/dev/null 2>&1; then
	echo "openssl is required to generate _env/langfuse.env" >&2
	exit 1
fi

rand_hex() {
	openssl rand -hex "$1"
}

base64_one_line() {
	printf '%s' "$1" | base64 | tr -d '\n'
}

project_public_key="pk-lf-local-dev"
project_secret_key="sk-lf-$(rand_hex 16)"
otel_basic_auth="$(base64_one_line "${project_public_key}:${project_secret_key}")"

cat <<ENV
# Generated by scripts/create-langfuse-env.sh.
# Local development only. Do not reuse these values outside your workstation.

LANGFUSE_NEXTAUTH_URL=http://localhost:3000
LANGFUSE_NEXTAUTH_SECRET=$(rand_hex 32)
LANGFUSE_SALT=$(rand_hex 16)
LANGFUSE_ENCRYPTION_KEY=$(rand_hex 32)
LANGFUSE_TELEMETRY_ENABLED=false
LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES=true

LANGFUSE_INIT_ORG_ID=fastapi-agent-blueprint
LANGFUSE_INIT_ORG_NAME=FastAPI Agent Blueprint
LANGFUSE_INIT_PROJECT_ID=local-dev
LANGFUSE_INIT_PROJECT_NAME=Local Development
LANGFUSE_INIT_PROJECT_PUBLIC_KEY=${project_public_key}
LANGFUSE_INIT_PROJECT_SECRET_KEY=${project_secret_key}
LANGFUSE_INIT_USER_EMAIL=admin@example.com
LANGFUSE_INIT_USER_NAME=Admin
LANGFUSE_INIT_USER_PASSWORD=lf-$(rand_hex 12)
LANGFUSE_OTEL_BASIC_AUTH=${otel_basic_auth}

LANGFUSE_POSTGRES_USER=postgres
LANGFUSE_POSTGRES_PASSWORD=lf-$(rand_hex 12)
LANGFUSE_POSTGRES_DB=postgres

LANGFUSE_CLICKHOUSE_USER=clickhouse
LANGFUSE_CLICKHOUSE_PASSWORD=lf-$(rand_hex 12)
LANGFUSE_CLICKHOUSE_CLUSTER_ENABLED=false

LANGFUSE_REDIS_AUTH=lf-$(rand_hex 12)
LANGFUSE_REDIS_TLS_ENABLED=false

LANGFUSE_MINIO_ROOT_USER=minio
LANGFUSE_MINIO_ROOT_PASSWORD=lf-$(rand_hex 12)
LANGFUSE_S3_BUCKET=langfuse
LANGFUSE_S3_REGION=auto
LANGFUSE_S3_BATCH_EXPORT_ENABLED=false
ENV
