<?php

// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
defined( 'ABSPATH' ) || exit;

/**
 * Migration: Create WooCommerce Tables
 * Creates boostbot_woocommerce_presets and boostbot_woocommerce_coupons tables.
 *
 * Schema matches the column sets expected by BoostBot_WooCommerce_Preset_Model
 * and BoostBot_WooCommerce_Coupon_Model.
 */
class BoostBot_Migration_Create_WooCommerce_Tables {

	/**
	 * Run the migration.
	 */
	public static function run() {
		$instance = new self();
		$instance->up();
	}

	/**
	 * Create the tables.
	 */
	public function up() {
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		// ── 1. boostbot_woocommerce_presets ──────────────────────────
		$table_presets = $wpdb->prefix . 'boostbot_woocommerce_presets';
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$sql_presets   = "CREATE TABLE $table_presets (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			slug varchar(255) DEFAULT NULL,
			description text,
			is_default tinyint(1) NOT NULL DEFAULT 0,
			is_system tinyint(1) NOT NULL DEFAULT 0,
			is_active tinyint(1) NOT NULL DEFAULT 1,
			language varchar(10) NOT NULL DEFAULT 'he',
			discovery_mode varchar(50) DEFAULT 'conversational',
			discovery_steps longtext,
			budget_ranges longtext,
			bundle_enabled tinyint(1) NOT NULL DEFAULT 1,
			bundle_min_products int NOT NULL DEFAULT 2,
			bundle_max_products int NOT NULL DEFAULT 5,
			bundle_discount_type varchar(20) DEFAULT 'percentage',
			bundle_discount_value decimal(10,2) DEFAULT 0,
			product_mode varchar(20) DEFAULT 'all',
			allowed_categories longtext,
			excluded_categories longtext,
			specific_product_ids longtext,
			preferred_product_ids longtext,
			excluded_product_ids longtext,
			include_on_sale tinyint(1) NOT NULL DEFAULT 1,
			include_variable tinyint(1) NOT NULL DEFAULT 1,
			include_featured_only tinyint(1) NOT NULL DEFAULT 0,
			min_stock int NOT NULL DEFAULT 0,
			min_rating decimal(3,2) DEFAULT 0,
			max_single_product_percent decimal(5,2) DEFAULT 50,
			discount_tiers longtext,
			max_discount_amount decimal(10,2) DEFAULT NULL,
			min_order_for_discount decimal(10,2) DEFAULT 0,
			show_original_prices tinyint(1) NOT NULL DEFAULT 1,
			show_savings tinyint(1) NOT NULL DEFAULT 1,
			show_per_product_savings tinyint(1) NOT NULL DEFAULT 0,
			round_prices tinyint(1) NOT NULL DEFAULT 0,
			round_to int DEFAULT 1,
			coupon_enabled tinyint(1) NOT NULL DEFAULT 0,
			coupon_mode varchar(20) DEFAULT 'manual',
			auto_coupon_id bigint(20) unsigned DEFAULT NULL,
			auto_select_best_coupon tinyint(1) NOT NULL DEFAULT 0,
			generate_unique_coupon tinyint(1) NOT NULL DEFAULT 0,
			generated_coupon_prefix varchar(20) DEFAULT 'BB',
			generated_coupon_discount decimal(10,2) DEFAULT 10,
			generated_coupon_expiry_days int DEFAULT 30,
			show_coupon_code tinyint(1) NOT NULL DEFAULT 1,
			show_coupon_value tinyint(1) NOT NULL DEFAULT 1,
			coupon_max_per_order int DEFAULT 1,
			allow_customization tinyint(1) NOT NULL DEFAULT 1,
			cart_redirect tinyint(1) NOT NULL DEFAULT 0,
			show_cart_button tinyint(1) NOT NULL DEFAULT 0,
			intro_message text,
			loading_message text,
			success_message text,
			empty_message text,
			error_message text,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY language (language),
			KEY is_active (is_active),
			KEY is_default (is_default),
			KEY slug (slug(191))
		) $charset_collate;";

		dbDelta( $sql_presets );

		// ── 2. boostbot_woocommerce_coupons ─────────────────────────
		$table_coupons = $wpdb->prefix . 'boostbot_woocommerce_coupons';
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$sql_coupons   = "CREATE TABLE $table_coupons (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			preset_id bigint(20) unsigned NOT NULL,
			wc_coupon_id bigint(20) unsigned NOT NULL,
			display_name varchar(255) DEFAULT NULL,
			description text,
			keywords text,
			priority int NOT NULL DEFAULT 0,
			min_cart_total decimal(10,2) DEFAULT NULL,
			max_cart_total decimal(10,2) DEFAULT NULL,
			applicable_categories longtext,
			is_active tinyint(1) NOT NULL DEFAULT 1,
			for_widget tinyint(1) NOT NULL DEFAULT 1,
			for_bundle tinyint(1) NOT NULL DEFAULT 1,
			language varchar(10) NOT NULL DEFAULT 'he',
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY preset_id (preset_id),
			KEY wc_coupon_id (wc_coupon_id),
			KEY language (language),
			KEY is_active (is_active),
			KEY for_widget (for_widget),
			KEY for_bundle (for_bundle),
			KEY priority (priority)
		) $charset_collate;";

		dbDelta( $sql_coupons );
	}
}
