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

if [ ! -d "./evibes" ]; then
  printf "\e[31m❌  Please run this script from the project's root (where the 'evibes' directory lives).\e[0m\n"
  exit 1
fi

PURPLE="\e[38;2;121;101;209m"
RESET="\e[0m"
GRAY="\e[90m"

SOURCE="${BASH_SOURCE[0]:-$0}"
SCRIPT_DIR="$(cd "$(dirname "$SOURCE")" && pwd)"
ART_PATH="$SCRIPT_DIR/../ASCII_ART_EVIBES"

if [ ! -f "$ART_PATH" ]; then
  printf "\e[31m❌  Could not find ASCII art at %s\e[0m\n" "$ART_PATH"
  exit 1
fi

while IFS= read -r line; do
  printf "%b%s%b\n" "$PURPLE" "$line" "$RESET"
done < "$ART_PATH"

printf "\n%b                                                                       by WISELESS TEAM%b\n\n" "$GRAY" "$RESET"

# prompt for a value with default
prompt_default() {
  local name="$1" default="$2" result
  read -rp "Enter $name [$default]: " result
  printf '%s\n' "${result:-$default}"
}

# prompt for a value, auto-generate hex if blank
prompt_autogen() {
  local name="$1" bytes="$2" result
  read -rp "Enter $name (leave blank to auto-generate): " result
  if [ -z "$result" ]; then
    # generate $bytes random bytes as hex
    result="$(head -c "$bytes" /dev/urandom | od -An -tx1 | tr -d ' \n')"
  fi
  printf '%s\n' "$result"
}

if [ -f .env ]; then
  printf "\e[33m.env already exists and will be overwritten.\e[0m\n"
  read -rp "Press Enter to continue or Ctrl+C to abort"
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')

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_SFTP_HOST=$(prompt_default 'DBBACKUP_SFTP_HOST' 'Your SFTP backup host')
DBBACKUP_SFTP_USER=$(prompt_default 'DBBACKUP_SFTP_USER' 'The username to use to log in to that host')
DBBACKUP_SFTP_PASS=$(prompt_default 'DBBACKUP_SFTP_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')

# Write to .env
{
  printf 'EVIBES_PROJECT_NAME="%s"\n' "$PROJECT_NAME"
  printf 'EVIBES_FRONTEND_DOMAIN="%s"\n' "$FRONTEND_DOMAIN"
  printf 'EVIBES_BASE_DOMAIN="%s"\n' "$BASE_DOMAIN"
  printf 'SENTRY_DSN="%s"\n' "$SENTRY_DSN"
  printf 'DEBUG=%s\n\n' "$DEBUG"

  printf 'SECRET_KEY="%s"\n' "$SECRET_KEY"
  printf 'JWT_SIGNING_KEY="%s"\n\n' "$JWT_SIGNING_KEY"

  printf 'ALLOWED_HOSTS="%s"\n' "$ALLOWED_HOSTS"
  printf 'CSRF_TRUSTED_ORIGINS="%s"\n' "$CSRF_TRUSTED_ORIGINS"
  printf 'CORS_ALLOWED_ORIGINS="%s"\n\n' "$CORS_ALLOWED_ORIGINS"

  printf 'POSTGRES_DB="%s"\n' "$POSTGRES_DB"
  printf 'POSTGRES_USER="%s"\n' "$POSTGRES_USER"
  printf 'POSTGRES_PASSWORD="%s"\n\n' "$POSTGRES_PASSWORD"

  printf 'DBBACKUP_SFTP_HOST="%s"\n' "$DBBACKUP_SFTP_HOST"
  printf 'DBBACKUP_SFTP_USER="%s"\n' "$DBBACKUP_SFTP_USER"
  printf 'DBBACKUP_SFTP_PASS="%s"\n\n' "$DBBACKUP_SFTP_PASS"

  printf 'ELASTIC_PASSWORD="%s"\n\n' "$ELASTIC_PASSWORD"

  printf 'REDIS_PASSWORD="%s"\n' "$REDIS_PASSWORD"
  printf 'CELERY_BROKER_URL="redis://:%s@redis:6379/0"\n' "$REDIS_PASSWORD"
  printf 'CELERY_RESULT_BACKEND="redis://:%s@redis:6379/0"\n\n' "$REDIS_PASSWORD"

  printf 'PROMETHEUS_USER="%s"\n' "$PROMETHEUS_USER"
  printf 'PROMETHEUS_PASSWORD="%s"\n\n' "$PROMETHEUS_PASSWORD"

  printf 'EMAIL_BACKEND="%s"\n' "$EMAIL_BACKEND"
  printf 'EMAIL_HOST="%s"\n' "$EMAIL_HOST"
  printf 'EMAIL_PORT="%s"\n' "$EMAIL_PORT"
  printf 'EMAIL_USE_TLS=%s\n' "$EMAIL_USE_TLS"
  printf 'EMAIL_USE_SSL=%s\n' "$EMAIL_USE_SSL"
  printf 'EMAIL_HOST_USER="%s"\n' "$EMAIL_HOST_USER"
  printf 'EMAIL_HOST_PASSWORD="%s"\n' "$EMAIL_HOST_PASSWORD"
  printf 'EMAIL_FROM="%s"\n\n' "$EMAIL_FROM"

  printf 'COMPANY_NAME="%s"\n' "$COMPANY_NAME"
  printf 'COMPANY_PHONE_NUMBER="%s"\n' "$COMPANY_PHONE_NUMBER"
  printf 'COMPANY_ADDRESS="%s"\n\n' "$COMPANY_ADDRESS"

  printf 'OPENAI_API_KEY="%s"\n\n' "$OPENAI_API_KEY"
  printf 'ABSTRACT_API_KEY="%s"\n\n' "$ABSTRACT_API_KEY"
  printf 'DEEPL_AUTH_KEY="%s"\n' "$DEEPL_AUTH_KEY"
} > .env

printf "\e[32m.env file generated with fresh values.\e[0m\n"
