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

# Run only from the deployed project root after backend/.env is completed.
cd "$(dirname "$0")/../backend"
test -f .env || { echo "Missing backend/.env"; exit 1; }
grep -q '^APP_ENV=production' .env || { echo "Set APP_ENV=production in backend/.env first"; exit 1; }
grep -q '^DB_DATABASE=' .env || { echo "Missing DB_DATABASE"; exit 1; }

composer install --no-dev --optimize-autoloader --no-interaction
if grep -q '^APP_KEY=$' .env; then php artisan key:generate --force; fi
php artisan migrate --seed --force
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
echo "Migration completed. Check https://api.shop.moyaplus.sa/api/v1/health"
