<?php
defined('ABSPATH') || exit;

function cf7_submissions_create_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'cf7_submissions';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE {$table_name} (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name text NOT NULL,
        email text NOT NULL,
        phone text NOT NULL,
        message text NOT NULL,
        submitted_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";

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

function cf7_submissions_maybe_create_table() {
    if (!get_option('cf7_submissions_table_created')) {
        cf7_submissions_create_table();
        update_option('cf7_submissions_table_created', 1);
    }
}
