<?php
/**
 * CleverReach WooCommerce Integration.
 *
 * @package CleverReach
 */

namespace CleverReach\WooCommerce\Migrations\V348;

use CleverReach\WooCommerce\Components\Repositories\Base_Repository;
use CleverReach\WooCommerce\Components\Util\Step;

/**
 * Class Add_Automation_Indexes
 *
 * @package CleverReach\WooCommerce\Migrations\V348
 */
class Add_Automation_Indexes extends Step {

	const CLASS_NAME = __CLASS__;

	/**
	 * Executes DB index creation
	 */
	public function execute() {
		$table = Base_Repository::get_table_name('cleverreach_wc_automation');

		$existing = array_column(
			$this->db->get_results("SHOW INDEX FROM $table", ARRAY_A),
			'Key_name'
		);

		if (!in_array('idx_type', $existing)) {
			$this->db->query("ALTER TABLE $table ADD INDEX idx_type(`type`)");
		}

		if (!in_array('idx_index_1', $existing)) {
			$this->db->query("ALTER TABLE $table ADD INDEX idx_index_1(`index_1`)");
		}
	}
}
