<?php
/**
 * SIMPLEST SAVE TEST - Tests saving a snippet directly
 * Run this to see if snippets can be saved to database
 */

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

echo '<h1>Simple Save Test</h1>';

global $wpdb;

// Test 1: Check if table exists
echo '<h2>Test 1: Check Table</h2>';
$table_name = $wpdb->prefix . 'pc_hfap_snippets';
$exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));

if ($table_name === $exists) {
    echo '<p style="color:green">✓ Table exists: ' . $table_name . '</p>';
} else {
    echo '<p style="color:red">✗ Table NOT found: ' . $table_name . '</p>';
    echo '<p>Creating table...</p>';
    
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        title varchar(255) NOT NULL,
        location enum('header','footer','body') NOT NULL DEFAULT 'header',
        code longtext NOT NULL,
        created_at datetime DEFAULT CURRENT_TIMESTAMP,
        updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY  (id)
    ) $wpdb->get_charset_collate();";
    
    dbDelta($sql);
    
    $exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));
    if ($table_name === $exists) {
        echo '<p style="color:green">✓ Table created!</p>';
    } else {
        echo '<p style="color:red">✗ Failed to create table</p>';
        die('Error: ' . $wpdb->last_error);
    }
}

// Test 2: Insert directly
echo '<h2>Test 2: Direct Insert</h2>';

$result = $wpdb->insert(
    $table_name,
    array(
        'title' => 'Direct Test ' . date('Y-m-d H:i:s'),
        'location' => 'header',
        'code' => '<script>console.log("Direct test");</script>'
    ),
    array('%s', '%s', '%s')
);

if ($result) {
    $insert_id = $wpdb->insert_id;
    echo '<p style="color:green">✓ Inserted! ID: ' . $insert_id . '</p>';
    
    // Test 3: Read back
    echo '<h2>Test 3: Read Back</h2>';
    $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $insert_id), ARRAY_A);
    
    if ($row) {
        echo '<p style="color:green">✓ Data retrieved:</p>';
        echo '<ul>';
        echo '<li>Title: ' . htmlspecialchars($row['title']) . '</li>';
        echo '<li>Location: ' . $row['location'] . '</li>';
        echo '<li>Code: ' . htmlspecialchars($row['code']) . '</li>';
        echo '</ul>';
        
        // Test 4: Delete
        echo '<h2>Test 4: Delete</h2>';
        $del = $wpdb->delete($table_name, array('id' => $insert_id), array('%d'));
        
        if ($del) {
            echo '<p style="color:green">✓ Deleted successfully!</p>';
        } else {
            echo '<p style="color:red">✗ Delete failed</p>';
            echo '<p>Error: ' . $wpdb->last_error . '</p>';
        }
    } else {
        echo '<p style="color:red">✗ Could not retrieve inserted data</p>';
    }
} else {
    echo '<p style="color:red">✗ Insert failed</p>';
    echo '<p>Error: ' . $wpdb->last_error . '</p>';
    echo '<p>Last query: ' . $wpdb->last_query . '</p>';
}

echo '<h2>Done!</h2>';
echo '<p><a href="' . admin_url('admin.php?page=pc-hfap-snippets') . '">Go to Snippets Page</a></p>';
echo '<p><a href="' . admin_url('admin.php?page=pc-hfap-add-snippet') . '">Add New Snippet</a></p>';
