<?php
/**
 * Debug tool to test snippet functionality
 */

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

// Check if user has permissions
if (!current_user_can('manage_options')) {
    die('You do not have sufficient permissions.');
}

echo '<h1>Snippet Debug Test</h1>';

// Include required files
require_once dirname(__FILE__) . '/includes/class-database.php';
require_once dirname(__FILE__) . '/includes/class-snippet.php';

echo '<h2>Test Results:</h2>';

// Test 1: Check database connection
echo '<h3>1. Database Connection</h3>';
global $wpdb;
echo '<p>Database Host: ' . DB_HOST . '</p>';
echo '<p>Database Name: ' . DB_NAME . '</p>';
echo '<p>Table Prefix: ' . $wpdb->prefix . '</p>';

// Test 2: Check if table exists
echo '<h3>2. Database Table</h3>';
$table_name = $wpdb->prefix . 'pc_hfap_snippets';
echo '<p>Expected Table: ' . $table_name . '</p>';

$table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));
if ($table_name === $table_exists) {
    echo '<p style="color: green;">✓ Table exists!</p>';
} else {
    echo '<p style="color: red;">✗ Table does not exist!</p>';
    echo '<p>Attempting to create table...</p>';
    PC_HFAP_Database::create_tables();
    
    // Check again
    $table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));
    if ($table_name === $table_exists) {
        echo '<p style="color: green;">✓ Table created successfully!</p>';
    } else {
        echo '<p style="color: red;">✗ Failed to create table!</p>';
        echo '<p>Last error: ' . $wpdb->last_error . '</p>';
    }
}

// Test 3: Try to insert a test snippet
echo '<h3>3. Insert Test Snippet</h3>';
$test_data = array(
    'title' => 'Debug Test Snippet - ' . date('Y-m-d H:i:s'),
    'location' => 'header',
    'code' => '<!-- Debug test code -->'
);

echo '<p>Testing snippet creation...</p>';
$snippet = new PC_HFAP_Snippet($test_data);
$result = $snippet->save();

if ($result) {
    echo '<p style="color: green;">✓ Snippet created successfully! ID: ' . $result . '</p>';
    
    // Test 4: Retrieve the snippet
    echo '<h3>4. Retrieve Snippet</h3>';
    $retrieved = PC_HFAP_Snippet::get_by_id($result);
    if ($retrieved) {
        echo '<p style="color: green;">✓ Snippet retrieved: ' . esc_html($retrieved->get_title()) . '</p>';
        
        // Test 5: Delete the snippet
        echo '<h3>5. Delete Snippet</h3>';
        $delete_result = $retrieved->delete();
        if ($delete_result) {
            echo '<p style="color: green;">✓ Snippet deleted successfully!</p>';
        } else {
            echo '<p style="color: red;">✗ Failed to delete snippet!</p>';
        }
    } else {
        echo '<p style="color: red;">✗ Failed to retrieve snippet!</p>';
    }
} else {
    echo '<p style="color: red;">✗ Failed to create snippet!</p>';
    echo '<p>Last error: ' . $wpdb->last_error . '</p>';
}

// Test 6: List all snippets
echo '<h3>6. List All Snippets</h3>';
$all_snippets = PC_HFAP_Snippet::get_all();
echo '<p>Total snippets in database: ' . count($all_snippets) . '</p>';

echo '<h2>Debug Complete</h2>';
echo '<p>If you see any red ✗ marks, check your WordPress debug.log for more details.</p>';
echo '<p>To run this test:</p>';
echo '<ol>';
echo '<li>Upload this file to your server</li>';
echo '<li>Access it via browser: yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/debug-test.php</li>';
echo '<li>Check the results</li>';
echo '</ol>';
