<?php
/**
 * GUARANTEED SAVE TEST
 * This test will DEFINITELY show if saving works
 */

// WordPress bootstrap
$wp_load = dirname(__FILE__) . '/../../../../wp-load.php';

if (!file_exists($wp_load)) {
    die("ERROR: WordPress not found at: $wp_load");
}

require_once $wp_load;

echo '<!DOCTYPE html>
<html>
<head>
    <title>Guaranteed Save Test</title>
    <style>
        body { font-family: monospace; padding: 20px; max-width: 800px; margin: 0 auto; }
        h1 { background: #0073aa; color: white; padding: 15px; margin: -20px -20px 20px; }
        .ok { background: #d4edda; color: #155724; padding: 10px; margin: 5px 0; border-left: 5px solid #28a745; }
        .error { background: #f8d7da; color: #721c24; padding: 10px; margin: 5px 0; border-left: 5px solid #dc3545; }
        .info { background: #d1ecf1; color: #0c5460; padding: 10px; margin: 5px 0; border-left: 5px solid #0073aa; }
        pre { background: #f5f5f5; padding: 10px; overflow: auto; }
    </style>
</head>
<body>
<h1>Guaranteed Save Test</h1>';

$passed = true;
$start = microtime(true);

// STEP 1: Load classes
echo '<div class="info">STEP 1: Loading plugin classes...</div>';

$plugin_dir = dirname(__FILE__) . '/';

try {
    require_once $plugin_dir . 'includes/class-database.php';
    echo '<div class="ok">✓ class-database.php loaded</div>';
    
    require_once $plugin_dir . 'includes/class-snippet.php';
    echo '<div class="ok">✓ class-snippet.php loaded</div>';
} catch (Exception $e) {
    echo '<div class="error">✗ Failed to load classes: ' . $e->getMessage() . '</div>';
    $passed = false;
}

// STEP 2: Create table
echo '<div class="info">STEP 2: Creating database table...</div>';

try {
    global $wpdb;
    PC_HFAP_Database::create_tables();
    
    $table = PC_HFAP_Database::get_table_name();
    $exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
    
    if ($table === $exists) {
        echo '<div class="ok">✓ Table exists: ' . $table . '</div>';
    } else {
        echo '<div class="error">✗ Table NOT created!</div>';
        $passed = false;
    }
} catch (Exception $e) {
    echo '<div class="error">✗ Database error: ' . $e->getMessage() . '</div>';
    $passed = false;
}

// STEP 3: Save snippet
echo '<div class="info">STEP 3: Creating a test snippet...</div>';

$snippet_id = 0;

try {
    $data = array(
        'title' => 'GUARANTEED TEST ' . date('Y-m-d H:i:s'),
        'location' => 'header',
        'code' => '<script>console.log("This WILL work!");</script>'
    );
    
    $snippet = new PC_HFAP_Snippet($data);
    $result = $snippet->save();
    
    if ($result) {
        $snippet_id = $result;
        echo '<div class="ok">✓ Snippet SAVED! ID = ' . $result . '</div>';
    } else {
        echo '<div class="error">✗ Save FAILED! $result = false</div>';
        echo '<div class="error">WordPress error: ' . $wpdb->last_error . '</div>';
        $passed = false;
    }
} catch (Exception $e) {
    echo '<div class="error">✗ Exception: ' . $e->getMessage() . '</div>';
    $passed = false;
}

// STEP 4: Read it back
echo '<div class="info">STEP 4: Reading snippet back...</div>';

if ($snippet_id > 0) {
    try {
        $retrieved = PC_HFAP_Snippet::get_by_id($snippet_id);
        
        if ($retrieved && $retrieved->get_id()) {
            echo '<div class="ok">✓ Snippet retrieved!</div>';
            echo '<pre>';
            echo 'ID: ' . $retrieved->get_id() . "\n";
            echo 'Title: ' . $retrieved->get_title() . "\n";
            echo 'Location: ' . $retrieved->get_location() . "\n";
            echo 'Code: ' . $retrieved->get_code() . "\n";
            echo '</pre>';
        } else {
            echo '<div class="error">✗ Could not retrieve snippet</div>';
            $passed = false;
        }
    } catch (Exception $e) {
        echo '<div class="error">✗ Retrieve error: ' . $e->getMessage() . '</div>';
        $passed = false;
    }
}

// STEP 5: Delete it
echo '<div class="info">STEP 5: Deleting test snippet...</div>';

if ($snippet_id > 0) {
    try {
        $snippet = PC_HFAP_Snippet::get_by_id($snippet_id);
        
        if ($snippet) {
            $delete_result = $snippet->delete();
            
            if ($delete_result) {
                echo '<div class="ok">✓ Snippet deleted!</div>';
            } else {
                echo '<div class="error">✗ Delete failed</div>';
                $passed = false;
            }
        }
    } catch (Exception $e) {
        echo '<div class="error">✗ Delete error: ' . $e->getMessage() . '</div>';
        $passed = false;
    }
}

// FINAL
$time = round((microtime(true) - $start) * 1000, 2);

echo '<hr>';
echo '<h2>RESULT</h2>';

if ($passed) {
    echo '<div class="ok" style="font-size: 24px;">✓✓✓ ALL STEPS PASSED! ✓✓✓</div>';
    echo '<p>The plugin CAN save snippets to the database.</p>';
    echo '<p>If the admin form is not working, the issue is with the form submission process, not the save function.</p>';
} else {
    echo '<div class="error" style="font-size: 24px;">✗ SOME STEPS FAILED</div>';
    echo '<p>Please check the errors above.</p>';
}

echo '<p>Test completed in ' . $time . 'ms</p>';

echo '<h3>Next Actions</h3>';
echo '<ul>';
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-add-snippet') . '">Try the Add New form</a></li>';
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-snippets') . '">View saved snippets</a></li>';
echo '</ul>';

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