<?php
/**
 * Plugin Name:       Sequensy
 * Plugin URI:        https://sequensy.com
 * Description:       Visual workflow automation for WordPress
 * Version:           2.0.0
 * Requires at least: 6.4
 * Requires PHP:      8.0
 * Author:            Summix
 * Author URI:        https://summix.io
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       sequensy
 * Domain Path:       /languages
 *
 * @package Sequensy
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Current plugin version.
 */
define( 'SEQUENSY_VERSION', '2.0.0' );

/**
 * Plugin directory path with trailing slash.
 */
define( 'SEQUENSY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

/**
 * Plugin URL with trailing slash.
 */
define( 'SEQUENSY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Load Composer autoloader for PSR-4 namespaced classes.
 */
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
	require_once __DIR__ . '/vendor/autoload.php';
}

/**
 * Load global helper functions.
 */
require_once __DIR__ . '/includes/functions/sequensy-api.php';

/**
 * Load Action Scheduler library.
 *
 * Loads early (before plugins_loaded) to ensure functions are available
 * when triggers fire. Action Scheduler uses plugins_loaded priority 0.
 */
function sequensy_load_action_scheduler() {
	if ( ! class_exists( 'ActionScheduler_Versions' ) ) {
		require_once plugin_dir_path( __FILE__ ) . 'vendor/action-scheduler/action-scheduler.php';
	}
}
sequensy_load_action_scheduler();

/**
 * Plugin activation hook.
 */
function sequensy_activate() {
	\Sequensy\Storage\WorkflowStorage::create_tables();

	if ( false === get_option( 'sequensy_async_execution' ) ) {
		add_option( 'sequensy_async_execution', true );
	}

	// Force package auto-discovery on next page load.
	\Sequensy\Packages\PackageLoader::delete_registry();

	delete_transient( 'sequensy_action_metadata' );
	delete_transient( 'sequensy_trigger_metadata' );
}
register_activation_hook( __FILE__, 'sequensy_activate' );

/**
 * Plugin deactivation hook.
 */
function sequensy_deactivate() {
	delete_transient( 'sequensy_action_metadata' );
	delete_transient( 'sequensy_trigger_metadata' );
}
register_deactivation_hook( __FILE__, 'sequensy_deactivate' );

/**
 * Get registered service providers.
 *
 * @return array List of service provider instances.
 */
function sequensy_get_service_providers() {
	return array(
		new \Sequensy\Providers\CoreServiceProvider(),
		new \Sequensy\Providers\RegistryServiceProvider(),
		new \Sequensy\Providers\AdminServiceProvider(),
		new \Sequensy\Providers\RestApiServiceProvider(),
		new \Sequensy\Providers\IntegrationServiceProvider(),
	);
}

/**
 * Register core services in the container using service providers.
 *
 * @param \Sequensy\Container $container  Service container instance.
 * @param array               $providers  Service provider instances.
 */
function sequensy_register_services( $container, $providers ) {
	foreach ( $providers as $provider ) {
		$provider->register( $container );
	}

	/**
	 * Allow other plugins (e.g. Sequensy Pro) to register services.
	 *
	 * @param \Sequensy\Container $container Service container instance.
	 */
	do_action( 'sequensy_register_services', $container );
}

/**
 * Boot service providers.
 *
 * @param \Sequensy\Container $container  Service container instance.
 * @param array               $providers  Service provider instances.
 */
function sequensy_boot_providers( $container, $providers ) {
	foreach ( $providers as $provider ) {
		$provider->boot( $container );
	}
}

/**
 * Initialize plugin using service providers.
 */
function sequensy_run() {
	$providers = sequensy_get_service_providers();
	$container = \Sequensy\Container::getInstance();
	sequensy_register_services( $container, $providers );
	sequensy_boot_providers( $container, $providers );

	/**
	 * Fires after Sequensy is fully loaded.
	 *
	 * Pro and add-on plugins should hook here to register extensions.
	 */
	do_action( 'sequensy_loaded' );
}
add_action( 'plugins_loaded', 'sequensy_run' );
