<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Create database table on activation
 *
 * Creates the 'admilogu_login_history' table in the database.
 * This table stores logs of failed login attempts, including IP address,
 * username, timestamp, and user agent.
 *
 * @param int|null $blog_id Optional. Blog ID for multisite support. Defaults to current blog.
 */
function admilogu_create_table($blog_id = null)
{
    global $wpdb;

    if (is_multisite() && $blog_id) {
        switch_to_blog($blog_id);
    }

    $table_name = $wpdb->prefix . 'admilogu_login_history';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        ip_address varchar(45) NOT NULL,
        username varchar(60) NOT NULL,
        status varchar(20) NOT NULL DEFAULT 'failed',
        time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
        user_agent text NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

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

    if (is_multisite() && $blog_id) {
        restore_current_blog();
    }
}
