#!/bin/bash

# ==============================================================================
# SEGYRIC VPN / LARAVEL BACKEND AUTOMATED DEPLOYMENT SCRIPT
# ==============================================================================
# Usage: ./deploy.sh
# ==============================================================================

echo -e "\e[34m==================================================\e[0m"
echo -e "\e[36m[*] Starting Segyric VPN Server Automated Deployment...\e[0m"
echo -e "\e[34m==================================================\e[0m"

# 0. Detect PHP 8.2+ binary on cPanel / Linux
PHP_BIN=""
if [ -x "/usr/local/bin/ea-php83" ]; then
    PHP_BIN="/usr/local/bin/ea-php83"
elif [ -x "/usr/local/bin/ea-php82" ]; then
    PHP_BIN="/usr/local/bin/ea-php82"
elif [ -x "/opt/cpanel/ea-php82/root/usr/bin/php" ]; then
    PHP_BIN="/opt/cpanel/ea-php82/root/usr/bin/php"
elif [ -x "/opt/cpanel/ea-php83/root/usr/bin/php" ]; then
    PHP_BIN="/opt/cpanel/ea-php83/root/usr/bin/php"
elif command -v php8.2 &> /dev/null; then
    PHP_BIN="php8.2"
elif command -v php &> /dev/null; then
    PHP_BIN="php"
fi

echo -e "\e[32m[0/7] Using PHP binary: $PHP_BIN ($($PHP_BIN -v 2>&1 | head -n 1))\e[0m"

# 1. Pull latest code if Git is initialized
if [ -d ".git" ]; then
    echo -e "\e[33m[1/7] Pulling latest updates from Git...\e[0m"
    git pull origin main || echo -e "\e[31m[!] Git pull skipped or clean.\e[0m"
else
    echo -e "\e[33m[1/7] Git directory not found. Skipping git pull...\e[0m"
fi

# 2. Check for .env file
if [ ! -f ".env" ]; then
    echo -e "\e[33m[2/7] .env file not found! Copying from .env.example...\e[0m"
    cp .env.example .env || true
    $PHP_BIN artisan key:generate || echo -e "\e[31m[!] Could not generate app key.\e[0m"
    echo -e "\e[31m[!] Please edit your .env file with production database and URL settings before re-running.\e[0m"
else
    echo -e "\e[32m[2/7] Production .env file verified.\e[0m"
fi

# 3. Detect and Install Composer Dependencies
echo -e "\e[33m[3/7] Checking Composer availability...\e[0m"
COMPOSER_CMD=""
if command -v composer &> /dev/null; then
    COMPOSER_CMD="composer"
elif [ -f "/usr/local/bin/composer" ]; then
    COMPOSER_CMD="$PHP_BIN /usr/local/bin/composer"
elif [ -f "/opt/cpanel/composer/bin/composer" ]; then
    COMPOSER_CMD="$PHP_BIN /opt/cpanel/composer/bin/composer"
elif [ -f "composer.phar" ]; then
    COMPOSER_CMD="$PHP_BIN composer.phar"
fi

if [ -n "$COMPOSER_CMD" ]; then
    echo -e "\e[32mUsing composer command: $COMPOSER_CMD\e[0m"
    $COMPOSER_CMD install --no-dev --optimize-autoloader || echo -e "\e[31m[!] Composer install had warnings/errors. Proceeding to next step...\e[0m"
else
    echo -e "\e[31m[!] Composer binary not found. Skipping composer install step...\e[0m"
fi

# 4. Run Database Migrations & Seeders
echo -e "\e[33m[4/7] Running database migrations & seeding 25+ real VPN servers...\e[0m"
$PHP_BIN artisan migrate --force || echo -e "\e[31m[!] Database migration skipped or failed. Check DB credentials in .env.\e[0m"
$PHP_BIN artisan db:seed --class=VpnServerSeeder --force || echo -e "\e[31m[!] Database seeding skipped or failed.\e[0m"

# 5. Optimize Caches
echo -e "\e[33m[5/7] Clearing & caching configuration, routes, and views...\e[0m"
$PHP_BIN artisan config:clear || true
$PHP_BIN artisan route:clear || true
$PHP_BIN artisan view:clear || true
$PHP_BIN artisan config:cache || echo -e "\e[31m[!] config:cache skipped.\e[0m"
$PHP_BIN artisan route:cache || echo -e "\e[31m[!] route:cache skipped.\e[0m"
$PHP_BIN artisan view:cache || echo -e "\e[31m[!] view:cache skipped.\e[0m"

# 6. Set Directory Permissions
echo -e "\e[33m[6/7] Setting file storage permissions...\e[0m"
chmod -R 775 storage bootstrap/cache 2>/dev/null || chmod -R 755 storage bootstrap/cache 2>/dev/null || echo -e "\e[31m[!] Could not change storage permissions.\e[0m"
if id "www-data" &>/dev/null; then
    chown -R www-data:www-data storage bootstrap/cache 2>/dev/null || true
fi

# 7. Reload Web Server if systemctl exists
echo -e "\e[33m[7/7] Reloading web services...\e[0m"
if command -v systemctl &> /dev/null; then
    sudo systemctl reload nginx 2>/dev/null || true
    sudo systemctl restart php8.2-fpm 2>/dev/null || true
fi

echo -e "\e[34m==================================================\e[0m"
echo -e "\e[32mâœ… Segyric VPN Server Deployment Process Completed!\e[0m"
echo -e "\e[34m==================================================\e[0m"


