<?php
/**
 * Create Schema on Activation
 *
 * @package MultiPOS - Point of Sale for WooCommerce
 * @version 1.0.0
 */

defined( 'ABSPATH' ) || exit();

if ( ! class_exists( 'DDWCPOS_Install' ) ) {
	/**
	 * Activation class
	 */
	class DDWCPOS_Install {
		/**
		 * On plugin activation function
		 *
		 * @return void
		 */
		public static function ddwcpos_on_plugin_activation() {
			// Trigger setup wizard for new installations.
			set_transient( 'ddfw_activation_redirect_devdiggers-multipos-for-woocommerce', true, 30 );

			if ( ! get_option( 'ddwcpos_installed_at' ) ) {
				update_option( 'ddwcpos_installed_at', time() );
			}

			self::ddwcpos_create_schema();
		}

		/**
		 * Create Schema
		 */
		public static function ddwcpos_create_schema() {
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';

			global $wpdb;

			$charset_collate = $wpdb->get_charset_collate();

			// The free version is single-outlet by design: the one outlet is stored as an
			// option (see DDWCPOS_Outlet_Helper), so no outlets/outletmeta tables are created.

			// Transactions table.
			$transactions_table = $wpdb->prefix . 'ddwcpos_transactions';
			$transactions = "CREATE TABLE IF NOT EXISTS $transactions_table (
				`id` bigint(20) NOT NULL AUTO_INCREMENT,
				`cashier_id` bigint(20) NOT NULL,
				`outlet_id` bigint(20) NOT NULL,
				`order_id` bigint(20) DEFAULT NULL,
				`in` varchar(200) NOT NULL,
				`out` varchar(200) NOT NULL,
				`method` varchar(200) NOT NULL,
				`reference` text NOT NULL,
				`date` datetime NOT NULL,
				PRIMARY KEY (id)
			) $charset_collate;";

            dbDelta( $transactions );

			self::ddwcpos_create_role();
		}

		/**
		 * Create POS Cashier role function
		 *
		 * @return void
		 */
		public static function ddwcpos_create_role() {
			if ( empty( get_role( 'ddwcpos_cashier' ) ) ) {
				add_role(
					'ddwcpos_cashier',
					'POS Cashier',
					[
						'read'    => true,
						'level_0' => true,
					]
				);
			}
		}
	}
}
