<?php
/**
 * ULTIMATE PLUGIN TEST
 */

require_once dirname(__FILE__) . '/../../../../wp-load.php';

header('Content-Type: text/html; charset=utf-8');

echo '<!DOCTYPE html>
<html>
<head>
<title>Ultimate Plugin Test</title>
<style>
body{font-family:sans-serif;max-width:800px;margin:40px auto;padding:20px}
h1{color:#23282d}
.test{padding:15px;margin:10px 0;border-radius:5px}
.pass{background:#d4edda;color:#155724;border:1px solid #c3e6cb}
.fail{background:#f8d7da;color:#721c24;border:1px solid #f5c6cb}
.info{background:#d1ecf1;color:#0c5460;border:1px solid #bee5eb}
</style>
</head>
<body>
<h1>Headers & Footers Plugin - Ultimate Test</h1>
<p>WordPress: ' . get_bloginfo('version') . ' | PHP: ' . PHP_VERSION . '</p>';

$all_passed = true;
$start_time = microtime(true);

// TEST 1: Files
echo '<div class="test info"><strong>TEST 1: Plugin Files</strong></div>';

$files = array(
    'Main plugin' => plugin_dir_path(__FILE__) . 'pc-headers-and-footers-and-ad-pixels-5ake.php',
    'Database class' => dirname(__FILE__) . '/includes/class-database.php',
    'Snippet class' => dirname(__FILE__) . '/includes/class-snippet.php',
    'Admin class' => dirname(__FILE__) . '/admin/class-admin.php',
    'Public class' => dirname(__FILE__) . '/public/class-public.php',
);

foreach ($files as $name => $path) {
    if (file_exists($path)) {
        echo "<p class='pass'>OK: $name exists</p>";
    } else {
        echo "<p class='fail'>FAIL: $name MISSING!</p>";
        $all_passed = false;
    }
}

// TEST 2: Classes
echo '<div class="test info"><strong>TEST 2: Classes</strong></div>';

if (class_exists('PC_HFAP_Database')) {
    echo "<p class='pass'>OK: PC_HFAP_Database class exists</p>";
} else {
    echo "<p class='fail'>FAIL: PC_HFAP_Database class not found</p>";
    $all_passed = false;
}

if (class_exists('PC_HFAP_Snippet')) {
    echo "<p class='pass'>OK: PC_HFAP_Snippet class exists</p>";
} else {
    echo "<p class='fail'>FAIL: PC_HFAP_Snippet class not found</p>";
    $all_passed = false;
}

// TEST 3: Database
echo '<div class="test info"><strong>TEST 3: Database Table</strong></div>';

try {
    global $wpdb;
    PC_HFAP_Database::create_tables();
    $table_name = PC_HFAP_Database::get_table_name();
    $table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));
    
    if ($table_name === $table_exists) {
        echo "<p class='pass'>OK: Table \'$table_name\' exists</p>";
        $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
        echo "<p class='info'>Current snippets: $count</p>";
    } else {
        echo "<p class='fail'>FAIL: Table does not exist!</p>";
        $all_passed = false;
    }
} catch (Exception $e) {
    echo "<p class='fail'>FAIL: Database error: " . $e->getMessage() . '</p>';
    $all_passed = false;
}

// TEST 4: CRUD
echo '<div class="test info"><strong>TEST 4: CRUD Operations</strong></div>';

$test_id = 0;

// CREATE
try {
    $data = array(
        'title' => 'Ultimate Test ' . time(),
        'location' => 'header',
        'code' => '<script>/* Test */</script>'
    );
    
    $snippet = new PC_HFAP_Snippet($data);
    $result = $snippet->save();
    
    if ($result) {
        $test_id = $result;
        echo "<p class='pass'>OK: CREATE - Snippet ID $result created</p>";
    } else {
        echo "<p class='fail'>FAIL: CREATE - Failed to create snippet</p>";
        echo "<p>Error: " . $wpdb->last_error . '</p>';
        $all_passed = false;
    }
} catch (Exception $e) {
    echo "<p class='fail'>FAIL: CREATE Exception: " . $e->getMessage() . '</p>';
    $all_passed = false;
}

// READ
if ($test_id > 0) {
    try {
        $snippet = PC_HFAP_Snippet::get_by_id($test_id);
        if ($snippet && $snippet->get_id()) {
            echo "<p class='pass'>OK: READ - Retrieved snippet</p>";
        } else {
            echo "<p class='fail'>FAIL: READ - Could not retrieve</p>";
            $all_passed = false;
        }
    } catch (Exception $e) {
        echo "<p class='fail'>FAIL: READ Exception</p>";
        $all_passed = false;
    }
    
    // UPDATE
    try {
        $snippet = PC_HFAP_Snippet::get_by_id($test_id);
        if ($snippet) {
            $snippet->set_title('Updated ' . time());
            $snippet->set_code('<style>/* Updated */</style>');
            $update_result = $snippet->save();
            
            if ($update_result !== false) {
                echo "<p class='pass'>OK: UPDATE - Snippet updated</p>";
            } else {
                echo "<p class='fail'>FAIL: UPDATE - Failed</p>";
                $all_passed = false;
            }
        }
    } catch (Exception $e) {
        echo "<p class='fail'>FAIL: UPDATE Exception</p>";
        $all_passed = false;
    }
    
    // DELETE
    try {
        $snippet = PC_HFAP_Snippet::get_by_id($test_id);
        if ($snippet) {
            $delete_result = $snippet->delete();
            if ($delete_result) {
                echo "<p class='pass'>OK: DELETE - Snippet deleted</p>";
            } else {
                echo "<p class='fail'>FAIL: DELETE - Failed</p>";
                $all_passed = false;
            }
        }
    } catch (Exception $e) {
        echo "<p class='fail'>FAIL: DELETE Exception</p>";
        $all_passed = false;
    }
} else {
    echo "<p class='fail'>FAIL: SKIPPED READ/UPDATE/DELETE (no ID)</p>";
    $all_passed = false;
}

// FINAL
$end_time = microtime(true);
$duration = round(($end_time - $start_time) * 1000, 2);

echo '<hr>';
echo '<div class="test" style="background:#23282d;color:white;text-align:center">';
echo '<h2>COMPLETED IN ' . $duration . 'ms</h2>';

if ($all_passed) {
    echo '<h1 style="color:#28a745">ALL TESTS PASSED</h1>';
} else {
    echo '<h1 style="color:#dc3545">SOME TESTS FAILED</h1>';
}

echo '</div>';

echo '<h3>Quick Links</h3>';
echo '<ul>';
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-snippets') . '">Admin Page</a></li>';
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-add-snippet') . '">Add New</a></li>';
echo '</ul>';

echo '</body></html>';
