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

source ./scripts/Unix/starter.sh

get_random_hex() {
  hexdump -v -e '/1 "%02x"' -n "$1" /dev/urandom
}

prompt_default() {
  printf "Enter %s [%s]: " "$1" "$2"
  read -r response
  if [ -z "${response//[[:space:]]/}" ]; then
    echo "$2"
  else
    echo "$response"
  fi
}

prompt_autogen() {
  printf "Enter %s (leave blank to auto-generate): " "$1"
  read -r response
  if [ -z "${response//[[:space:]]/}" ]; then
    get_random_hex "$2"
  else
    echo "$response"
  fi
}

if [ -f .env ]; then
  echo ".env already exists and will be overwritten." >&2
  printf "Press Enter to continue or Ctrl+C to abort"
  read -r
fi

PROJECT_NAME=$(prompt_default EVIBES_PROJECT_NAME eVibes)
FRONTEND_DOMAIN=$(prompt_default EVIBES_FRONTEND_DOMAIN evibes.com)
BASE_DOMAIN=$(prompt_default EVIBES_BASE_DOMAIN evibes.com)
SENTRY_DSN=$(prompt_default SENTRY_DSN "")
DEBUG=$(prompt_default DEBUG 1)
TIME_ZONE=$(prompt_default TIME_ZONE "Europe/London")

SECRET_KEY=$(prompt_autogen SECRET_KEY 32)
JWT_SIGNING_KEY=$(prompt_autogen JWT_SIGNING_KEY 64)

ALLOWED_HOSTS=$(prompt_default ALLOWED_HOSTS "evibes.com api.evibes.com b2b.evibes.com")
CSRF_TRUSTED_ORIGINS=$(prompt_default CSRF_TRUSTED_ORIGINS "https://evibes.com https://api.evibes.com https://www.evibes.com https://b2b.evibes.com")
CORS_ALLOWED_ORIGINS=$CSRF_TRUSTED_ORIGINS

POSTGRES_DB=$(prompt_default POSTGRES_DB evibes)
POSTGRES_USER=$(prompt_default POSTGRES_USER evibes_user)
POSTGRES_PASSWORD=$(prompt_autogen POSTGRES_PASSWORD 16)

DBBACKUP_TYPE=$(prompt_default DBBACKUP_TYPE "Your backup connection type")
DBBACKUP_HOST=$(prompt_default DBBACKUP_HOST "Your SFTP backup host")
DBBACKUP_USER=$(prompt_default DBBACKUP_USER "The username to use to log in to that host")
DBBACKUP_PASS=$(prompt_default DBBACKUP_PASS "The password to use to log in to that host")

ELASTIC_PASSWORD=$(prompt_autogen ELASTIC_PASSWORD 16)
REDIS_PASSWORD=$(prompt_autogen REDIS_PASSWORD 16)

PROMETHEUS_USER=$(prompt_default PROMETHEUS_USER evibes)
PROMETHEUS_PASSWORD=$(prompt_autogen PROMETHEUS_PASSWORD 16)

EMAIL_BACKEND=$(prompt_default EMAIL_BACKEND django.core.mail.backends.smtp.EmailBackend)
EMAIL_HOST=$(prompt_default EMAIL_HOST smtp.whatever.evibes.com)
EMAIL_PORT=$(prompt_default EMAIL_PORT 465)
EMAIL_USE_TLS=$(prompt_default EMAIL_USE_TLS 0)
EMAIL_USE_SSL=$(prompt_default EMAIL_USE_SSL 1)
EMAIL_HOST_USER=$(prompt_default EMAIL_HOST_USER your-email-user@whatever.evibes.com)
EMAIL_FROM=$EMAIL_HOST_USER
EMAIL_HOST_PASSWORD=$(prompt_default EMAIL_HOST_PASSWORD SUPERSECRETEMAILHOSTPASSWORD)

COMPANY_NAME=$(prompt_default COMPANY_NAME "eVibes, Inc.")
COMPANY_PHONE_NUMBER=$(prompt_default COMPANY_PHONE_NUMBER "+888888888888")
COMPANY_ADDRESS=$(prompt_default COMPANY_ADDRESS "The place that does not exist")

OPENAI_API_KEY=$(prompt_default OPENAI_API_KEY "Haha, really?")
ABSTRACT_API_KEY=$(prompt_default ABSTRACT_API_KEY "Haha, really? x2")
DEEPL_AUTH_KEY=$(prompt_default DEEPL_AUTH_KEY "Haha, really? x3")

cat > .env <<EOF
EVIBES_PROJECT_NAME="$PR
