<?php
/**
 * Pro Database components registrar.
 *
 * @package   EDD\Pro\CartRecovery\Database
 * @copyright Copyright (c) 2025, Sandhills Development, LLC
 * @license   https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since     3.6.5
 */

namespace EDD\Pro\CartRecovery\Database;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\EventManagement\SubscriberInterface;

/**
 * Registers Pro-only database components.
 */
class Components implements SubscriberInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function get_subscribed_events() {
		return array(
			'edd_setup_components' => 'register',
		);
	}

	/**
	 * Register components on edd_setup_components.
	 *
	 * @since 3.6.5
	 */
	public function register() {
		// Prevent persistent object caches (Redis, Memcached) from caching ACR query
		// results across requests. Direct $wpdb bulk operations in Detection and
		// Repository bypass BerlinDB's cache invalidation, causing stale reads.
		wp_cache_add_non_persistent_groups(
			array(
				'edd-acr_carts',
				'edd-acr_carts-by-session_id',
				'edd-acr_emails',
				'edd-acr_journey_events',
			)
		);

		edd_register_component(
			'abandoned_cart',
			array(
				'schema' => '\EDD\Pro\CartRecovery\Database\Schemas\RecoveryCarts',
				'table'  => '\EDD\Pro\CartRecovery\Database\Tables\RecoveryCarts',
				'query'  => '\EDD\Pro\CartRecovery\Database\Queries\RecoveryCart',
				'object' => '\EDD\Pro\CartRecovery\Carts\Cart',
				'meta'   => false,
			)
		);

		edd_register_component(
			'recovery_email',
			array(
				'schema' => '\EDD\Pro\CartRecovery\Database\Schemas\RecoveryEmails',
				'table'  => '\EDD\Pro\CartRecovery\Database\Tables\RecoveryEmails',
				'query'  => '\EDD\Pro\CartRecovery\Database\Queries\RecoveryEmail',
				'object' => '\EDD\Pro\CartRecovery\Emails\Email',
				'meta'   => false,
			)
		);

		edd_register_component(
			'journey_event',
			array(
				'schema' => '\EDD\Pro\CartRecovery\Database\Schemas\JourneyEvents',
				'table'  => '\EDD\Pro\CartRecovery\Database\Tables\JourneyEvents',
				'query'  => '\EDD\Pro\CartRecovery\Database\Queries\JourneyEvent',
				'object' => '\EDD\Pro\CartRecovery\Database\Rows\JourneyEvent',
				'meta'   => false,
			)
		);
	}
}
