#!/usr/bin/env bash
# Writes release/env-for-server.txt: the live site's settings with a fresh APP_KEY.
# Fill in the database details, upload it into the app folder, and rename it to ".env".
# Never commit it (release/ is git-ignored). Run once; keep the same key afterwards.
#
#   bash scripts/make-production-env.sh
set -euo pipefail

ROOT="$(git rev-parse --show-toplevel)"
OUT="$ROOT/release/env-for-server.txt"
mkdir -p "$ROOT/release"

if [ -f "$OUT" ]; then
  echo "⚠  $OUT already exists. Not overwriting (the APP_KEY must stay the same once the site is live)."
  exit 1
fi

KEY="$(cd "$ROOT" && php artisan key:generate --show --no-ansi)"

cat > "$OUT" <<EOF
APP_NAME=FirstSittings
APP_ENV=production
APP_KEY=$KEY
APP_DEBUG=false
APP_URL=https://firstsittings.co.za
APP_TIMEZONE=Africa/Johannesburg
APP_LOCALE=en_ZA
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_ZA

LOG_CHANNEL=daily
LOG_LEVEL=warning

# ── Database: from cPanel → MySQL® Databases ───────────────────────────────
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=CHANGE_ME_cpaneluser_firstsittings
DB_USERNAME=CHANGE_ME_cpaneluser_fsapp
DB_PASSWORD=CHANGE_ME

SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=true
SESSION_SECURE_COOKIE=true

CACHE_STORE=database
# No background worker on cPanel yet: run jobs straight away.
QUEUE_CONNECTION=sync
FILESYSTEM_DISK=local

# ── Email: invites are logged until this is set up (admins can copy invite links meanwhile).
# To send for real, use a cPanel email account (cPanel → Email Accounts → Connect Devices):
#   MAIL_MAILER=smtp
#   MAIL_HOST=mail.firstsittings.co.za
#   MAIL_PORT=465
#   MAIL_SCHEME=smtps
#   MAIL_USERNAME=hello@firstsittings.co.za
#   MAIL_PASSWORD=...
MAIL_MAILER=log
MAIL_FROM_ADDRESS="hello@firstsittings.co.za"
MAIL_FROM_NAME="FirstSittings"
EOF

echo "✔ Wrote $OUT. Fill in the DB_ lines, then follow docs/deploy-manual.md."
