#!/bin/bash
set -e

echo "=== [1/5] Installing Composer Dependencies (PHP 8.4) ==="
composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction

echo "=== [2/5] Running Database Migrations ==="
php artisan migrate --force

echo "=== [3/5] Optimizing Cache & Routes ==="
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

echo "=== [4/5] Restarting Horizon Workers ==="
# Sinyal terminate ke horizon agar supervisor/systemd menjalankan ulang dengan kode terbaru
php artisan horizon:terminate || true

echo "=== [5/5] Ensuring Storage & Cache Permissions ==="
chmod -R 775 storage bootstrap/cache

echo "=== Deployment Finished Successfully! ==="
