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

cd "$(dirname "$0")/.."

if [[ ! -f .env ]]; then
    echo "Missing .env. Configure .env.production.example before deployment." >&2
    exit 1
fi

if grep -Eq '^APP_ENV=(local|testing)$|^APP_DEBUG=true$|^SEED_DEMO_DATA=true$' .env; then
    echo "Unsafe production environment settings detected in .env." >&2
    exit 1
fi

php artisan migrate:status
php artisan down --retry=60

restore_service() {
    php artisan up >/dev/null 2>&1 || true
}
trap restore_service EXIT

php artisan migrate --force
php artisan storage:link
php artisan optimize
php artisan queue:restart
php artisan up
trap - EXIT

php artisan about --only=environment
echo "Deployment commands completed. Run the verification checklist in DEPLOYMENT.md."
