<?php
/**
 * Core Service Provider
 *
 * Registers core services: storage, logger, executor, and filter evaluator.
 *
 * @package Sequensy
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Providers;

use Sequensy\Container;
use Sequensy\Engine\ActionQueueProcessor;
use Sequensy\Engine\ContextSerializer;
use Sequensy\Engine\ExecutionGuard;
use Sequensy\Engine\FilterEvaluator;
use Sequensy\Engine\QueueManager;
use Sequensy\Engine\VariableReplacer;
use Sequensy\Engine\WorkflowExecutor;
use Sequensy\Storage\ActivityLogger;
use Sequensy\Storage\ExecutionStorage;
use Sequensy\Storage\VersionStorage;
use Sequensy\Storage\WorkflowStorage;
use Sequensy\Validation\WorkflowValidator;

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

/**
 * Class CoreServiceProvider
 *
 * Provides core services for workflow storage, execution, and logging.
 */
class CoreServiceProvider implements ServiceProvider {

	/**
	 * Register core services in the container.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function register( Container $container ): void {
		// Core storage services (injecting $wpdb for testability).
		$container->register(
			'workflow_storage',
			function () {
				global $wpdb;
				return new WorkflowStorage( $wpdb );
			}
		);

		$container->register(
			'execution_storage',
			function () {
				global $wpdb;
				return new ExecutionStorage( $wpdb );
			}
		);

		$container->register(
			'version_storage',
			function () {
				global $wpdb;
				return new VersionStorage( $wpdb );
			}
		);

		// Activity logger.
		$container->register(
			'logger',
			function () {
				global $wpdb;
				return new ActivityLogger( $wpdb );
			}
		);

		// Context serializer (standalone, no dependencies).
		$container->register(
			'context_serializer',
			function () {
				return new ContextSerializer();
			}
		);

		// Queue manager (depends on logger, execution_storage, workflow_storage, context_serializer).
		$container->register(
			'queue_manager',
			function ( $c ) {
				return new QueueManager(
					$c->get( 'logger' ),
					$c->get( 'execution_storage' ),
					$c->get( 'workflow_storage' ),
					$c->get( 'context_serializer' )
				);
			}
		);

		// Variable replacer (standalone, no dependencies).
		$container->register(
			'variable_replacer',
			function () {
				return new VariableReplacer();
			}
		);

		// Workflow executor (depends on logger, action_registry, and variable_replacer).
		$container->register(
			'workflow_executor',
			function ( $c ) {
				return new WorkflowExecutor(
					$c->get( 'logger' ),
					$c->get( 'action_registry' ),
					$c->get( 'variable_replacer' )
				);
			}
		);

		// Filter evaluator.
		$container->register(
			'filter_evaluator',
			function () {
				return new FilterEvaluator();
			}
		);

		// Execution guard for loop prevention (singleton).
		$container->register(
			'execution_guard',
			function () {
				return new ExecutionGuard();
			}
		);

		// Workflow validator (depends on registries).
		$container->register(
			'workflow_validator',
			function ( $c ) {
				return new WorkflowValidator(
					$c->get( 'trigger_registry' ),
					$c->get( 'action_registry' )
				);
			}
		);
	}

	/**
	 * Boot the core service provider.
	 *
	 * Registers Action Scheduler queue processor hooks.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function boot( Container $container ): void {
		// Register per-action queue processor hooks.
		ActionQueueProcessor::register_hooks();

		// Schedule weekly cleanup of stale data.
		if ( ! wp_next_scheduled( 'sequensy_cleanup' ) ) {
			wp_schedule_event( time(), 'weekly', 'sequensy_cleanup' );
		}

		add_action(
			'sequensy_cleanup',
			function () use ( $container ) {
				$container->get( 'logger' )->prune( 90 );
				$container->get( 'execution_storage' )->cleanup_stale( 24 );
			}
		);
	}
}
