#!/bin/bash

# Server Setup Script for Domain Rotator
# Run this script directly on the server

echo "🚀 Setting up Domain Rotator on rotator-jago.online"
echo "=================================================="

# Update system
echo "📦 Updating system packages..."
apt update && apt upgrade -y

# Install PHP 8.2 and extensions
echo "🐘 Installing PHP 8.2..."
apt install -y software-properties-common
add-apt-repository ppa:ondrej/php -y
apt update
apt install -y php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip php8.2-bcmath php8.2-json php8.2-cli

# Install Composer
echo "🎼 Installing Composer..."
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

# Install MySQL
echo "🗄️ Installing MySQL..."
apt install -y mysql-server

# Install Nginx
echo "🌐 Installing Nginx..."
apt install -y nginx

# Install Node.js and npm
echo "📦 Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt install -y nodejs

# Install other utilities
echo "🔧 Installing utilities..."
apt install -y unzip git curl wget

# Start and enable services
echo "🔄 Starting services..."
systemctl start php8.2-fpm
systemctl enable php8.2-fpm
systemctl start mysql
systemctl enable mysql
systemctl start nginx
systemctl enable nginx

# Secure MySQL installation
echo "🔒 Securing MySQL..."
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SecureRootPassword123!';"
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DROP DATABASE IF EXISTS test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -e "FLUSH PRIVILEGES;"

# Create database and user for Laravel
echo "🗃️ Creating database..."
mysql -u root -p'SecureRootPassword123!' -e "CREATE DATABASE IF NOT EXISTS domain_rotator;"
mysql -u root -p'SecureRootPassword123!' -e "CREATE USER IF NOT EXISTS 'domain_rotator'@'localhost' IDENTIFIED BY 'SecurePassword123!';"
mysql -u root -p'SecureRootPassword123!' -e "GRANT ALL PRIVILEGES ON domain_rotator.* TO 'domain_rotator'@'localhost';"
mysql -u root -p'SecureRootPassword123!' -e "FLUSH PRIVILEGES;"

# Create app directory
echo "📁 Creating application directory..."
mkdir -p /var/www/domain-rotator
chown -R www-data:www-data /var/www/domain-rotator

# Configure Nginx
echo "🌐 Configuring Nginx..."
cat > /etc/nginx/sites-available/rotator-jago.online << 'NGINXEOF'
server {
    listen 80;
    listen [::]:80;
    server_name rotator-jago.online www.rotator-jago.online;
    root /var/www/domain-rotator/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
NGINXEOF

# Enable site
ln -sf /etc/nginx/sites-available/rotator-jago.online /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default

# Test Nginx configuration
nginx -t

# Reload Nginx
systemctl reload nginx

# Setup firewall
echo "🔥 Configuring firewall..."
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

echo ""
echo "✅ SERVER SETUP COMPLETED!"
echo "=================================================="
echo "📋 Installed:"
echo "   - PHP 8.2 with extensions"
echo "   - Composer"
echo "   - MySQL Server"
echo "   - Nginx"
echo "   - Node.js & npm"
echo "   - Database: domain_rotator"
echo "   - User: domain_rotator"
echo ""
echo "🔧 Next Steps:"
echo "   1. Upload your Laravel application to /var/www/domain-rotator"
echo "   2. Run: composer install"
echo "   3. Run: php artisan migrate --seed"
echo "   4. Setup SSL with: certbot --nginx -d rotator-jago.online"
echo ""
echo "✅ Server is ready for Domain Rotator deployment!"
