#!/usr/bin/env bash
# ==============================================================================
# PeopleFlow HRIS - Production Deployment Script
# Usage: ./deploy.sh
# ==============================================================================
set -e

echo "🚀 [1/7] Enabling maintenance mode..."
php artisan down --render="errors::503" --secret="peopleflow-deploy-bypass-key" || true

echo "📦 [2/7] Pulling latest code changes..."
if [ -d .git ]; then
    git pull origin main
fi

echo "📦 [3/7] Installing production composer dependencies..."
composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction

echo "🗄️ [4/7] Running database migrations..."
php artisan migrate --force

echo "⚡ [5/7] Optimizing Laravel caches..."
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

echo "🔒 [6/7] Setting directory permissions..."
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache

echo "✨ [7/7] Disabling maintenance mode..."
php artisan up

echo "✅ PeopleFlow HRIS deployed successfully!"
