<?php
/**
 * Fired during plugin activation.
 *
 * @link       https://wpgenie.org
 * @since      1.0.0
 *
 * @package    Mysterx
 * @subpackage Mysterx/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Mysterx
 * @subpackage Mysterx/includes
 * @author     wpgenie <info@wpgenie.org>
 */
class Mysterx_Activator {

	/**
	 * Short Description. (use period)
	 *
	 * Long Description.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {

		global $wpdb;

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$charset_collate = $wpdb->get_charset_collate();

		$sql1 = 'CREATE TABLE IF NOT EXISTS ' . MYSTERX_TBL_ITEMS . " (

			`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			`product_id` bigint(20) NOT NULL,
			`reward_id` bigint(20) NOT NULL,
			`weight` bigint(20) DEFAULT 1,
			`qty` bigint(20) DEFAULT 0,
			`order` bigint(20) DEFAULT 0,

			PRIMARY KEY (`id`) USING BTREE,
			INDEX `product_rew` (`product_id`, `reward_id`) USING BTREE

		) {$charset_collate};";

		dbDelta( $sql1 );

		$sql2 = 'CREATE TABLE IF NOT EXISTS ' . MYSTERX_TBL_LOG . " (

			`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			`product_id` bigint(20) NOT NULL,
			`user_id` bigint(20) DEFAULT 0,
			`order_id` bigint(20) DEFAULT 0,
			`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

			PRIMARY KEY (`id`) USING BTREE,
			INDEX `idx_productid_userid` (`product_id`, `user_id`) USING BTREE

		) {$charset_collate};";

		dbDelta( $sql2 );

		$sql3 = 'CREATE TABLE IF NOT EXISTS ' . MYSTERX_TBL_OPEN_LOG . " (

			`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			`user_id` bigint(20) DEFAULT 0,
			`order_id` bigint(20) DEFAULT 0,
			`product_id` bigint(20) DEFAULT 0,
			`reward_id` bigint(20) DEFAULT 0,
			`opened_at` datetime DEFAULT CURRENT_TIMESTAMP,

			PRIMARY KEY (`id`) USING BTREE

		) {$charset_collate};";

		dbDelta( $sql3 );
	}
}
