<?php
/**
 * Installer
 *
 * Runs on plugin activation to create DB tables and execute one-off migrations.
 * Also handles plugin deactivation cleanup.
 *
 * @package MOMCS_Sync
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common;

use MOMCS_Sync\Includes\Common\Models\Multichannels_Model;
use MOMCS_Sync\Includes\Common\Models\Authentication_Details_Model;
use MOMCS_Sync\Includes\Common\Models\Product_Mapping_Model;
use MOMCS_Sync\Includes\Common\Services\User_Service;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Installer
 *
 * Handles plugin installation and deactivation tasks.
 */
class Installer {

	/**
	 * All user and license options to clean on uninstall.
	 *
	 * @var array<string>
	 */
	private const LICENSE_OPTIONS = array(
		'momcs_miniorange_user',       // Legacy plain text user option.
		'bW93Y3BzX3VzZXJfZGV0YWlscw==', // Encoded user details (base64 of momcs_user_details).
		'momcs_user_display_details', // Display details for UI.
		'momcs_lk',                    // License key.
		'momcs_led',                   // Encrypted license expiry date.
		'momcs_tla',                   // Trial license status.
		'momcs_license_expired',       // License expired flag.
		'momcs_ln_check_t',            // Last license check timestamp.
		'momcs_exp_notice_close',      // Expiry notice dismissed flag.
	);

	/**
	 * Run all installation tasks.
	 *
	 * @return void
	 */
	public static function install(): void {
		self::create_db_tables();

		// Trigger plugin activation hook for premium features.
		do_action( 'momcs_plugin_activated' );
	}

	/**
	 * Run cleanup tasks on plugin deactivation.
	 *
	 * Clears all user credentials, license data, and scheduled tasks
	 * to ensure a clean slate if the plugin is reactivated.
	 *
	 * @return void
	 */
	public static function uninstall(): void {
		// Clear all user and license data.
		self::clear_user_data();

		// Unschedule all plugin-related Action Scheduler tasks.
		self::unschedule_tasks();

		// Trigger plugin deactivation hook for premium features.
		do_action( 'momcs_plugin_deactivated' );
	}

	/**
	 * Create all custom DB tables required by the plugin.
	 *
	 * @return void
	 */
	private static function create_db_tables(): void {
		Multichannels_Model::create_table();
		Authentication_Details_Model::create_table();
		Product_Mapping_Model::create_table();
	}

	/**
	 * Clear all user and license related options.
	 *
	 * This ensures that when the plugin is reactivated, the user
	 * must re-login and re-verify their license.
	 *
	 * @return void
	 */
	private static function clear_user_data(): void {
		// Delete all license and user options.
		foreach ( self::LICENSE_OPTIONS as $option ) {
			delete_option( $option );
		}

		// Also clear via User_Service for any additional cleanup.
		$user_service = new User_Service();
		$user_service->clear_user();
	}

	/**
	 * Unschedule all plugin Action Scheduler tasks.
	 *
	 * @return void
	 */
	private static function unschedule_tasks(): void {
		if ( ! function_exists( 'as_unschedule_all_actions' ) ) {
			return;
		}

		// Plugin-specific Action Scheduler hooks.
		$hooks = array(
			'momcs_sync_process_bulk',
			'momcs_check_feed_status',
			'momcs_unlist_process_bulk',
			'momcs_fetch_orders_background',
			'momcs_scheduled_order_sync_execute',
			'momcs_sync_single_marketplace',
			'momcs_unlist_single_marketplace',
			'momcs_poll_feed_status',
			'momcs_build_feed_chunk',
			'momcs_scheduled_sync',
			'momcs_scheduled_order_sync',
			'momcs_scheduled_sync_execute',
		);

		foreach ( $hooks as $hook ) {
			as_unschedule_all_actions( $hook );
		}
	}
}
