<?php

/**
 * Fired during plugin activation
 *
 * @link       https://www.eurostocks.com
 * @since      1.0.0
 *
 * @package    Eurostocks
 * @subpackage Eurostocks/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Eurostocks
 * @subpackage Eurostocks/includes
 * @author     EuroStocks <info@eurostocks.com>
 */
class Eurostocks_Activator {

	/**
	 *
	 * @since    1.0.0
	 */
	public static function activate() {

		if (! wp_next_scheduled ( 'eurostocks_clean_ip_history' ) ) {
			 wp_schedule_event( time(), 'hourly', 'eurostocks_clean_ip_history' );
		 }

		 if (! wp_next_scheduled ( 'eurostocks_update_model_scheduler', [] )) {
			wp_schedule_event( time(), 'monthly', 'eurostocks_update_model_scheduler', [] );
		}

		$current_db_version = get_option('eurostocks_db_version');
		if($current_db_version != '1.0'){

			global $wpdb;
			$eurostocks_db_version = '1.0';
			$charset_collate = $wpdb->get_charset_collate();
			$table_name_eurostocks_ip = $wpdb->prefix . 'eurostocks_ip';

			$sql_eurostocks_ip = $wpdb->prepare("CREATE TABLE %i (
				id mediumint(11) NOT NULL AUTO_INCREMENT,
				  remote_addr text DEFAULT '',
				  forwarded_for text DEFAULT '',
				  blocked mediumint(3) DEFAULT 0,
				registration_date datetime DEFAULT CURRENT_TIMESTAMP,
				PRIMARY KEY  (id)
			  );", $table_name_eurostocks_ip);

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

			add_option( 'eurostocks_db_version', $eurostocks_db_version );
		}

		// Ensure latest DB schema updates (e.g. analytics blocked flag) are applied on activation.
		if ( ! class_exists( 'Eurostocks_Admin' ) ) {
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-eurostocks-admin.php';
		}
		$admin_updater = new Eurostocks_Admin( 'eurostocks', EUROSTOCKS_VERSION );
		$admin_updater->eurostocks_update_db_check();

	}

}
