#!/usr/bin/env bash
#
# Run from inside the extracted project root, e.g.:
#   cd /home/medianwe/bingo-app && bash deploy.sh
#
# Idempotent — safe to re-run after pulling new code or changing .env.

set -euo pipefail

cd "$(dirname "$0")"

if [[ ! -f .env ]]; then
    echo ".env not found at $(pwd)/.env"
    echo "Create it first (see deploy notes), then re-run this script."
    exit 1
fi

if ! grep -q '^APP_KEY=base64:' .env; then
    echo "==> Generating APP_KEY"
    php artisan key:generate --force
fi

echo "==> Setting permissions"
chmod -R 755 .
chmod -R 775 storage bootstrap/cache
chmod 775 database
[[ -f database/database.sqlite ]] && chmod 664 database/database.sqlite

echo "==> Clearing stale caches"
php artisan optimize:clear

echo "==> Building production caches"
php artisan optimize
php artisan view:cache

echo "==> Sanity check"
php artisan about --only=environment 2>/dev/null | head -20 || true

echo
echo "Done. Open https://bingo.levelupmkt.online/ to verify."
