<?php
// Registrar tabla de clics
function bpbooster_create_clicks_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . "bpbooster_clicks";
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        button_type VARCHAR(20) NOT NULL,
        post_id BIGINT(20) NOT NULL,
        click_time DATETIME DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id)
    ) $charset_collate;";

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta($sql);
}
register_activation_hook(__FILE__, 'bpbooster_create_clicks_table');

// Registrar clic por AJAX
add_action('wp_ajax_nopriv_bpbooster_register_click', 'bpbooster_register_click');
add_action('wp_ajax_bpbooster_register_click', 'bpbooster_register_click');

function bpbooster_register_click() {
    global $wpdb;
    $button_type = sanitize_text_field($_POST['button_type']);
    $post_id = intval($_POST['post_id']);
    $table_name = $wpdb->prefix . "bpbooster_clicks";

    $wpdb->insert($table_name, [
        'button_type' => $button_type,
        'post_id' => $post_id,
    ]);

    wp_send_json_success();
}
