#!/bin/bash

# Quick Server Configuration for Timeout Fix
# Run this script on production server as root or with sudo

echo "========================================"
echo "Fixing Request Timeout Configuration"
echo "========================================"
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Detect PHP version
PHP_VERSION=$(php -v | head -n 1 | cut -d " " -f 2 | cut -d "." -f 1,2)
echo -e "${YELLOW}Detected PHP Version: ${PHP_VERSION}${NC}"

# Backup existing php.ini
PHP_INI="/etc/php/${PHP_VERSION}/fpm/php.ini"
if [ -f "$PHP_INI" ]; then
    echo "Backing up php.ini..."
    sudo cp "$PHP_INI" "$PHP_INI.backup.$(date +%Y%m%d_%H%M%S)"
    echo -e "${GREEN}✓ Backup created${NC}"
else
    echo -e "${RED}✗ PHP INI not found at $PHP_INI${NC}"
    exit 1
fi

# Update PHP Configuration
echo ""
echo "Updating PHP Configuration..."
sudo sed -i 's/^max_execution_time = .*/max_execution_time = 300/' "$PHP_INI"
sudo sed -i 's/^max_input_time = .*/max_input_time = 300/' "$PHP_INI"
sudo sed -i 's/^memory_limit = .*/memory_limit = 512M/' "$PHP_INI"
echo -e "${GREEN}✓ PHP configuration updated${NC}"

# Restart PHP-FPM
echo ""
echo "Restarting PHP-FPM..."
sudo systemctl restart php${PHP_VERSION}-fpm
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ PHP-FPM restarted successfully${NC}"
else
    echo -e "${RED}✗ Failed to restart PHP-FPM${NC}"
    exit 1
fi

# Detect web server (Nginx or Apache)
if systemctl is-active --quiet nginx; then
    echo ""
    echo "Detected: Nginx"
    echo "Updating Nginx configuration..."
    
    # Find the site configuration
    NGINX_CONF="/etc/nginx/sites-available/default"
    if [ ! -f "$NGINX_CONF" ]; then
        echo -e "${YELLOW}! Using custom config location${NC}"
        echo "Please manually add these lines to your Nginx site config:"
        echo ""
        echo "fastcgi_read_timeout 300;"
        echo "fastcgi_send_timeout 300;"
        echo "proxy_read_timeout 300;"
        echo "proxy_connect_timeout 300;"
        echo "proxy_send_timeout 300;"
        echo ""
    else
        # Backup nginx config
        sudo cp "$NGINX_CONF" "$NGINX_CONF.backup.$(date +%Y%m%d_%H%M%S)"
        
        # Test nginx config
        sudo nginx -t
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✓ Nginx configuration valid${NC}"
            sudo systemctl reload nginx
            echo -e "${GREEN}✓ Nginx reloaded${NC}"
        else
            echo -e "${RED}✗ Nginx configuration test failed${NC}"
            echo "Please check Nginx configuration manually"
        fi
    fi
    
elif systemctl is-active --quiet apache2; then
    echo ""
    echo "Detected: Apache"
    echo "Please manually add 'Timeout 300' to your Apache VirtualHost configuration"
    echo "Then run: sudo systemctl reload apache2"
else
    echo -e "${YELLOW}! No web server detected or not running${NC}"
fi

# Show current PHP settings
echo ""
echo "========================================"
echo "Current PHP Settings:"
echo "========================================"
php -i | grep -E "max_execution_time|max_input_time|memory_limit"

echo ""
echo "========================================"
echo -e "${GREEN}Configuration Update Complete!${NC}"
echo "========================================"
echo ""
echo "Next steps:"
echo "1. Deploy the updated Laravel code"
echo "2. Test the resend button with a sample SO"
echo "3. Monitor logs: tail -f /var/www/html/storage/logs/laravel.log"
echo ""
