#!/usr/bin/env bash
# ElykiasPay — étapes serveur post-transfert (OVH mutualisé, SSH).
# Usage :  cd ~/elykiaspay && bash deploy.sh
# PHP :    PHP=php8.3 bash deploy.sh   (adapter au binaire OVH si besoin)
set -e
PHP="${PHP:-php8.3}"
cd "$(dirname "$0")"

echo "==> Dépendances (production)"
if command -v composer >/dev/null 2>&1; then
    composer install --no-dev --optimize-autoloader
else
    [ -f composer.phar ] || "$PHP" -r "copy('https://getcomposer.org/installer','ci.php');" && "$PHP" ci.php --quiet
    "$PHP" composer.phar install --no-dev --optimize-autoloader
fi

echo "==> Fichier .env"
[ -f .env ] || cp .env.production .env
grep -q '^APP_KEY=base64' .env || "$PHP" artisan key:generate --force

echo "==> Migrations"
"$PHP" artisan migrate --force

echo "==> Mise en cache config/routes/vues"
"$PHP" artisan config:cache
"$PHP" artisan route:cache
"$PHP" artisan view:cache

echo "==> Permissions"
chmod -R 775 storage bootstrap/cache

echo "==> Déploiement terminé. Vérifiez : curl https://VOTRE-DOMAINE/up"
