<?php
/**
 * Deployment Verification Script
 * Tests all critical admin functionality before cPanel deployment
 */

session_start();

// Simulate admin session for testing
$_SESSION['is_admin'] = true;

$testResults = [];
$errors = [];

echo "<!DOCTYPE html>
<html>
<head>
    <title>Deployment Verification</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { max-width: 1000px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; }
        h1 { color: #333; border-bottom: 3px solid #F07E2D; padding-bottom: 10px; }
        .test-group { margin: 20px 0; }
        .test-group h2 { color: #667eea; font-size: 18px; margin-top: 20px; }
        .test { margin: 10px 0; padding: 10px; background: #f9f9f9; border-left: 4px solid #ddd; }
        .pass { border-left-color: #4caf50; color: #2e7d32; }
        .fail { border-left-color: #f44336; color: #c62828; }
        .code { font-family: monospace; background: #f0f0f0; padding: 2px 6px; border-radius: 3px; }
        .summary { margin-top: 30px; padding: 15px; background: #f0f7ff; border: 2px solid #667eea; border-radius: 8px; }
        .summary h3 { margin-top: 0; color: #667eea; }
        .status-pass { color: #4caf50; font-weight: bold; }
        .status-fail { color: #f44336; font-weight: bold; }
    </style>
</head>
<body>
<div class='container'>
    <h1>🚀 Deployment Verification Report</h1>";

require_once __DIR__ . '/helpers/database_helper.php';

// Test 1: DatabaseHelper can be loaded
echo "<div class='test-group'>
    <h2>1. Core Infrastructure Tests</h2>";

$test1 = class_exists('DatabaseHelper');
$status = $test1 ? 'pass' : 'fail';
echo "<div class='test $status'>";
echo $test1 ? "✓ " : "✗ ";
echo "DatabaseHelper class loaded successfully</div>";
if (!$test1) $errors[] = "DatabaseHelper class not found";

// Test 2: Data directory access
$test2 = is_writable(DatabaseHelper::getDataDir());
$status = $test2 ? 'pass' : 'fail';
echo "<div class='test $status'>";
echo $test2 ? "✓ " : "✗ ";
echo "Data directory is writable: " . DatabaseHelper::getDataDir() . "</div>";
if (!$test2) $errors[] = "Data directory not writable";

// Test 3: Load products
echo "</div><div class='test-group'>
    <h2>2. Products Data Tests</h2>";

try {
    $products = DatabaseHelper::loadJson('products.json', []);
    echo "<div class='test pass'>✓ Products loaded successfully (" . count($products) . " items)</div>";
    $testResults['products_load'] = true;
} catch (Exception $e) {
    echo "<div class='test fail'>✗ Failed to load products: " . $e->getMessage() . "</div>";
    $errors[] = "Products loading failed";
    $testResults['products_load'] = false;
}

// Test 4: Load carousel
echo "</div><div class='test-group'>
    <h2>3. Carousel Data Tests</h2>";

try {
    $carousel = DatabaseHelper::loadJson('carousel.json', []);
    echo "<div class='test pass'>✓ Carousel loaded successfully</div>";
    $testResults['carousel_load'] = true;
} catch (Exception $e) {
    echo "<div class='test fail'>✗ Failed to load carousel: " . $e->getMessage() . "</div>";
    $errors[] = "Carousel loading failed";
    $testResults['carousel_load'] = false;
}

// Test 5: Load site config
echo "</div><div class='test-group'>
    <h2>4. Site Configuration Tests</h2>";

try {
    $siteConfig = DatabaseHelper::loadJson('site_config.json', []);
    echo "<div class='test pass'>✓ Site config loaded successfully</div>";
    echo "<div class='test'> - Site title: <code>" . htmlspecialchars($siteConfig['title'] ?? 'Not set') . "</code></div>";
    $testResults['siteconfig_load'] = true;
} catch (Exception $e) {
    echo "<div class='test fail'>✗ Failed to load site config: " . $e->getMessage() . "</div>";
    $errors[] = "Site config loading failed";
    $testResults['siteconfig_load'] = false;
}

// Test 6: Save test (create a test file)
echo "</div><div class='test-group'>
    <h2>5. Save Operations Tests</h2>";

try {
    $testData = [
        'test' => true,
        'timestamp' => date('Y-m-d H:i:s'),
        'environment' => php_uname()
    ];
    $saved = DatabaseHelper::saveJson('_test_deployment.json', $testData);
    if ($saved) {
        echo "<div class='test pass'>✓ Successfully saved test data file</div>";
        // Clean up
        unlink(DatabaseHelper::getDataDir() . '/_test_deployment.json');
        echo "<div class='test pass'>✓ Test file cleaned up</div>";
        $testResults['save_test'] = true;
    } else {
        echo "<div class='test fail'>✗ Failed to save test data</div>";
        $errors[] = "Save operation failed";
        $testResults['save_test'] = false;
    }
} catch (Exception $e) {
    echo "<div class='test fail'>✗ Save test failed: " . $e->getMessage() . "</div>";
    $errors[] = "Save operation exception: " . $e->getMessage();
    $testResults['save_test'] = false;
}

// Test 7: Load categories
echo "</div><div class='test-group'>
    <h2>6. Categories Tests</h2>";

try {
    $categories = DatabaseHelper::loadJson('categories.json', []);
    echo "<div class='test pass'>✓ Categories loaded successfully (" . count($categories) . " categories)</div>";
    $testResults['categories_load'] = true;
} catch (Exception $e) {
    echo "<div class='test fail'>✗ Failed to load categories: " . $e->getMessage() . "</div>";
    $errors[] = "Categories loading failed";
    $testResults['categories_load'] = false;
}

// Test 8: Uploads directory
echo "</div><div class='test-group'>
    <h2>7. File Upload Infrastructure Tests</h2>";

$uploadsDir = DatabaseHelper::getUploadsDir();
$uploadsWritable = is_writable($uploadsDir);
$status = $uploadsWritable ? 'pass' : 'fail';
echo "<div class='test $status'>";
echo $uploadsWritable ? "✓ " : "✗ ";
echo "Uploads directory exists and is writable: " . $uploadsDir . "</div>";
if (!$uploadsWritable) $errors[] = "Uploads directory not writable";

// Summary
echo "</div><div class='summary'>
    <h3>📊 Verification Summary</h3>";

$passCount = count(array_filter($testResults, fn($v) => $v === true));
$totalTests = count($testResults);

echo "<p>Tests Passed: <span class='status-pass'>$passCount/$totalTests</span></p>";

if (empty($errors)) {
    echo "<p style='font-size: 18px; color: #4caf50;'>✓ <strong>All tests passed! Ready for cPanel deployment.</strong></p>";
} else {
    echo "<p style='font-size: 18px; color: #f44336;'>✗ <strong>Some tests failed:</strong></p>";
    echo "<ul>";
    foreach ($errors as $error) {
        echo "<li style='color: #f44336;'>" . htmlspecialchars($error) . "</li>";
    }
    echo "</ul>";
}

echo "</div></div></body></html>";
?>
