<?php
/**
 * Cart Recovery Reports Registration
 *
 * @package     EDD\Pro\CartRecovery\Admin\Reports
 * @copyright   Copyright (c) 2026, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.6.5
 */

namespace EDD\Pro\CartRecovery\Admin\Reports;

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

use EDD\EventManagement\SubscriberInterface;

/**
 * Reports Loader Class.
 *
 * @since 3.6.5
 */
class Loader implements SubscriberInterface {

	/**
	 * Gets the subscribed events.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public static function get_subscribed_events(): array {
		return array(
			'edd_reports_init' => array( 'register', 50 ),
			'edd_get_reports'  => array( 'hide_report', 999 ),
		);
	}

	/**
	 * Registers the Cart Recovery report and endpoints.
	 *
	 * This report is "internal" and only displayed on the custom Cart Recovery Analytics screen.
	 * It's hidden from the main Reports tabs via the edd_get_reports filter.
	 *
	 * @since 3.6.5
	 * @param \EDD\Reports\Data\Report_Registry $reports Report registry.
	 */
	public function register( $reports ) {
		try {
			$tiles = array(
				'acr_carts_tracked'     => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\CartsTracked',
				'acr_abandoned'         => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\Abandoned',
				'acr_recovered'         => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\Recovered',
				'acr_recovered_revenue' => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\RecoveredRevenue',
				'acr_recovery_rate'     => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\RecoveryRate',
				'acr_emails_sent'       => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\EmailsSent',
				'acr_open_rate'         => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\OpenRate',
				'acr_click_rate'        => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Tiles\ClickRate',
			);

			$charts = array(
				'acr_recovered_revenue_chart' => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Charts\RecoveredRevenue',
				'acr_emails_sent_chart'       => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Charts\EmailsSent',
				'acr_emails_opened_chart'     => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Charts\EmailsOpened',
				'acr_emails_clicked_chart'    => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Charts\EmailsClicked',
				'acr_conversion_funnel_chart' => 'EDD\Pro\CartRecovery\Admin\Reports\Endpoints\Charts\ConversionFunnel',
			);

			// Register the internal report with restrictive capability.
			$reports->add_report(
				'cart_recovery_analytics',
				array(
					'label'      => __( 'Cart Recovery', 'easy-digital-downloads' ),
					'icon'       => 'email-alt',
					'priority'   => 9999,
					'capability' => 'manage_shop_settings',
					'endpoints'  => array(
						'tiles'  => array_keys( $tiles ),
						'charts' => array_keys( $charts ),
					),
					'filters'    => array( 'dates' ),
				)
			);

			// Instantiate all tile endpoint classes (registers them with the system).
			foreach ( $tiles as $class ) {
				new $class( $reports );
			}

			// Instantiate all chart endpoint classes (registers them with the system).
			foreach ( $charts as $class ) {
				new $class( $reports );
			}
		} catch ( \EDD_Exception $exception ) {
			edd_debug_log_exception( $exception );
		}
	}

	/**
	 * Hides the Cart Recovery Analytics report from the main Reports tabs.
	 *
	 * This report is only meant to be displayed on the custom Cart Recovery screen.
	 *
	 * @since 3.6.5
	 * @param array $reports Registered reports.
	 * @return array Filtered reports.
	 */
	public function hide_report( $reports ) {
		unset( $reports['cart_recovery_analytics'] );

		return $reports;
	}
}
