#!/bin/bash

echo "=========================================="
echo "  Laravel Server Diagnostics"
echo "=========================================="
echo ""

# Check Laravel log
echo "1. Checking Laravel Error Log:"
echo "----------------------------"
if [ -f "storage/logs/laravel.log" ]; then
    echo "Last 50 lines of laravel.log:"
    tail -n 50 storage/logs/laravel.log
else
    echo "✗ Laravel log file not found!"
fi

echo ""
echo ""

# Check permissions
echo "2. Checking Permissions:"
echo "----------------------------"
echo "storage directory:"
ls -la storage/

echo ""
echo "bootstrap/cache directory:"
ls -la bootstrap/cache/

echo ""
echo ""

# Check PHP errors
echo "3. Checking PHP Configuration:"
echo "----------------------------"
php -v
echo ""
php -m | grep -E "(pdo|mysql|mbstring|openssl|json)"

echo ""
echo ""

# Check .env
echo "4. Checking .env file:"
echo "----------------------------"
if [ -f ".env" ]; then
    echo "✓ .env file exists"
    echo "APP_DEBUG: $(grep APP_DEBUG .env)"
    echo "APP_ENV: $(grep APP_ENV .env)"
    echo "DB_HOST: $(grep DB_HOST .env)"
else
    echo "✗ .env file not found!"
fi

echo ""
echo ""

# Check if composer dependencies installed
echo "5. Checking Composer Dependencies:"
echo "----------------------------"
if [ -d "vendor" ]; then
    echo "✓ vendor directory exists"
else
    echo "✗ vendor directory not found! Run: composer install"
fi

echo ""
echo ""

# Check Apache/Nginx error log
echo "6. Checking Web Server Logs:"
echo "----------------------------"
if [ -f "/var/log/httpd/error_log" ]; then
    echo "Last 20 lines of Apache error log:"
    tail -n 20 /var/log/httpd/error_log
elif [ -f "/var/log/apache2/error.log" ]; then
    echo "Last 20 lines of Apache error log:"
    tail -n 20 /var/log/apache2/error.log
else
    echo "Apache error log not found in standard locations"
fi

echo ""
echo "=========================================="
