<?php

class Wildrobot_Logistra_Migration
{

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct($plugin_name, $version)
	{

		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}

	public function run_migrations()
	{
		$migrations = get_option('wildrobot_logistra_migrations', []);

		if (!in_array("cleanup_7_11_0", $migrations)) {
			self::cleanup_7_11_0();
		}
	}




	public static function cleanup_7_11_0()
	{
		try {
			global $wpdb;

			// Remove all shipping relation options
			$wpdb->query(
				$wpdb->prepare(
					"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
					'logistra-robots-shipping-relation%'
				)
			);

			// Remove specific old token
			delete_option('logistra-robots-token');

			// List of options that should keep autoload='yes'
			$keep_autoload = array(
				'wildrobot_logistra_pickuppoint_cart',
				'wildrobot_logistra_pickuppoint_checkout',
				'wildrobot_logistra_picklist_page',
				'wildrobot_logistra_picklist_active',
				'wildrobot_logistra_service_partner_select_default_value',
				'wildrobot_logistra_pickuppoint_checkout_inline_hook'
			);

			// Update autoload for all wildrobot_logistra options except the ones in keep_autoload
			$wpdb->query(
				$wpdb->prepare(
					"UPDATE {$wpdb->options} 
					SET autoload = 'no' 
					WHERE option_name LIKE %s 
					AND option_name NOT IN ('" . implode("','", $keep_autoload) . "')",
					'wildrobot_logistra_%'
				)
			);

			// Mark migration as complete
			$migrations = get_option('wildrobot_logistra_migrations', []);
			array_push($migrations, "cleanup_7_11_0");
			update_option("wildrobot_logistra_migrations", $migrations);

			// Add success notice
			add_action('admin_notices', function () {
				$class = 'notice notice-success';
				$message = __('Wildrobot Logistra: Opprydding av innstillinger fullført', 'wildrobot-logistra');
				printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message));
			});
		} catch (\Throwable $error) {
			add_action('admin_notices', function () use ($error) {
				$class = 'notice notice-error';
				$message = __('Kunne ikke fullføre opprydding av innstillinger. Feilmelding: ' . $error->getMessage(), 'wildrobot-logistra');
				printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message));
			});

			$logger = new WC_Logger();
			$context = ['source' => 'wildrobot-logistra-migration'];
			$logger->error($error->getMessage(), $context);
		}
	}
}
