#!/usr/bin/env bash
#
# Run the warm-flags-cache Rust CLI against the local dev environment.
#
# Targets the dockerized services started by ./bin/start:
#   - Postgres on localhost:5432 (posthog/posthog)
#   - Redis on localhost:6379 (db 1 for flags, matching feature-flags service)
#   - MinIO on localhost:19000 (bucket "posthog")
#
# Prerequisites:
#   - ./bin/start running (or at least db, redis7, objectstorage containers up)
#
# Usage:
#   bin/warm-flags-cache --team-ids 1 --no-stagger
#   bin/warm-flags-cache                           # warm all teams with flags
#   bin/warm-flags-cache --help
#
# Pass --release to build in release mode (first run is slow, subsequent fast).
# Everything else is forwarded to the binary.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

CARGO_PROFILE=()
BIN_ARGS=()
for arg in "$@"; do
    if [ "$arg" = "--release" ]; then
        CARGO_PROFILE=(--release)
    else
        BIN_ARGS+=("$arg")
    fi
done

# Local dev defaults — mirror bin/start-rust-service feature-flags, plus the
# MinIO endpoint/creds that the object storage client needs.
export READ_DATABASE_URL="${READ_DATABASE_URL:-postgres://posthog:posthog@localhost:5432/posthog}"
export REDIS_URL="${REDIS_URL:-redis://localhost:6379/}"
export FLAGS_REDIS_URL="${FLAGS_REDIS_URL:-redis://localhost:6379/1}"
export OBJECT_STORAGE_BUCKET="${OBJECT_STORAGE_BUCKET:-posthog}"
export OBJECT_STORAGE_REGION="${OBJECT_STORAGE_REGION:-us-east-1}"
export OBJECT_STORAGE_ENDPOINT="${OBJECT_STORAGE_ENDPOINT:-http://localhost:19000}"
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-object_storage_root_user}"
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-object_storage_root_password}"
# .envrc sets RUST_LOG to suppress flox noise (e.g. "flox=error,..."). Once RUST_LOG
# has any value, EnvFilter's default directive no longer applies — so the binary's
# logs would be silenced. Append our directive so warm_flags_cache=info always
# wins, while preserving any flox suppression the user wants.
if [ -z "${RUST_LOG:-}" ]; then
    export RUST_LOG="info"
elif [[ "$RUST_LOG" != *"warm_flags_cache="* ]]; then
    export RUST_LOG="${RUST_LOG},warm_flags_cache=info"
fi
export RUST_BACKTRACE="${RUST_BACKTRACE:-1}"

# Strip user:password@ from URLs before printing — the local defaults are safe
# but operators sometimes override these with real DSNs, and terminal scrollback
# / session recordings shouldn't capture credentials.
redact_dsn() {
    # scheme://user:pass@host… → scheme://***@host…
    printf '%s\n' "$1" | sed -E 's#(://)[^@/]*@#\1***@#'
}

echo ">> READ_DATABASE_URL=$(redact_dsn "$READ_DATABASE_URL")"
echo ">> FLAGS_REDIS_URL=$(redact_dsn "$FLAGS_REDIS_URL")"
echo ">> OBJECT_STORAGE_ENDPOINT=$(redact_dsn "$OBJECT_STORAGE_ENDPOINT") (bucket=$OBJECT_STORAGE_BUCKET)"

cd "$REPO_ROOT/rust"
exec cargo run "${CARGO_PROFILE[@]}" -p feature-flags --features warm-flags-cache --bin warm-flags-cache -- "${BIN_ARGS[@]}"
