<?php
/**
 * Al-Runan Backend Deployment Script
 * 
 * This script helps verify the backend is ready for deployment
 * and provides environment checks.
 */

header('Content-Type: text/plain');

echo "Al-Runan Backend Deployment Check\n";
echo "==================================\n\n";

// Check PHP version
$phpVersion = phpversion();
echo "PHP Version: $phpVersion\n";
if (version_compare($phpVersion, '7.4.0', '>=')) {
    echo "✓ PHP version is compatible (7.4+ required)\n";
} else {
    echo "✗ PHP version is too old. Please upgrade to 7.4 or higher.\n";
}

// Check required extensions
$requiredExtensions = ['pdo', 'pdo_mysql', 'json', 'mbstring', 'openssl'];
echo "\nChecking required PHP extensions:\n";
foreach ($requiredExtensions as $ext) {
    if (extension_loaded($ext)) {
        echo "✓ $ext extension is loaded\n";
    } else {
        echo "✗ $ext extension is missing\n";
    }
}

// Check directory permissions
$writableDirs = ['storage', 'storage/logs'];
echo "\nChecking directory permissions:\n";
foreach ($writableDirs as $dir) {
    $path = __DIR__ . '/' . $dir;
    if (is_writable($path)) {
        echo "✓ $dir is writable\n";
    } else {
        echo "✗ $dir is not writable (set permissions to 755)\n";
    }
}

// Check if .htaccess exists
echo "\nChecking configuration files:\n";
if (file_exists(__DIR__ . '/.htaccess')) {
    echo "✓ .htaccess file exists\n";
} else {
    echo "✗ .htaccess file is missing\n";
}

if (file_exists(__DIR__ . '/config.php')) {
    echo "✓ config.php file exists\n";
} else {
    echo "✗ config.php file is missing\n";
}

// Database connection test (if credentials are set)
echo "\nDatabase Configuration:\n";
if (file_exists(__DIR__ . '/config.php')) {
    require_once __DIR__ . '/config.php';
    
    if (defined('DB_HOST') && defined('DB_NAME') && defined('DB_USER')) {
        echo "Database settings found in config.php\n";
        echo "Host: " . DB_HOST . "\n";
        echo "Database: " . DB_NAME . "\n";
        echo "User: " . DB_USER . "\n";
        
        // Test connection
        try {
            $db = new PDO(
                "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME,
                DB_USER,
                DB_PASS
            );
            echo "✓ Database connection successful\n";
        } catch (PDOException $e) {
            echo "✗ Database connection failed: " . $e->getMessage() . "\n";
            echo "  This is normal during initial deployment - use the installation wizard\n";
        }
    } else {
        echo "Database settings not configured - use installation wizard\n";
    }
}

echo "\nDeployment Instructions:\n";
echo "1. Upload entire backend folder to: https://alrunan.bereelkenya.co.ke/backend/\n";
echo "2. Run installation wizard: https://alrunan.bereelkenya.co.ke/backend/install/install.html\n";
echo "3. Update mobile app API URL to: https://alrunan.bereelkenya.co.ke/backend/api\n";
echo "4. Test all API endpoints\n";

echo "\nInstallation Wizard URLs:\n";
echo "- Main Installation: https://alrunan.bereelkenya.co.ke/backend/install/install.html\n";
echo "- Admin Management: https://alrunan.bereelkenya.co.ke/backend/install/admin-management.html\n";

echo "\nAPI Test Endpoints:\n";
echo "- Products: https://alrunan.bereelkenya.co.ke/backend/api/products\n";
echo "- Auth Test: https://alrunan.bereelkenya.co.ke/backend/api/auth/status\n";

echo "\nDeployment completed successfully!\n";
?>