<?php

/**
 * Fired during plugin activation
 *
 * @link       https://wpiron.com
 * @since      1.0.0
 *
 * @package    iron-security
 * @subpackage iron-security/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    iron-security
 * @subpackage iron-security/includes
 * @author     WPIron <info@wpiron.com>
 */
class iron_securi_Activator {

	/**
	 * Short Description. (use period)
	 *
	 * Runs on plugin activation. Creates database tables, flushes rewrite rules.
	 * Also handles version checking and updates.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {
		$activator = new iron_securi_Activator();
		$activator->create_access_log_table();
		flush_rewrite_rules();
	}

	/**
	 * Create the access log table in the database
	 * 
	 * @since 1.0.0
	 */
	public function create_access_log_table() {
		global $wpdb;
		$charset_collate = $wpdb->get_charset_collate();
		$table_name = $wpdb->prefix . 'iron_security_logs';

		if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
			$sql = "CREATE TABLE $table_name (
				id bigint(20) NOT NULL AUTO_INCREMENT,
				log_type varchar(50) NOT NULL,
				user_id bigint(20) DEFAULT NULL,
				username varchar(60) DEFAULT NULL,
				ip_address varchar(45) NOT NULL,
				message text NOT NULL,
				status varchar(20) DEFAULT NULL,
				date_created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
				extra_data longtext DEFAULT NULL,
				PRIMARY KEY  (id),
				KEY log_type (log_type),
				KEY user_id (user_id),
				KEY status (status),
				KEY date_created (date_created)
			) $charset_collate;";

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

}
