<?php
/**
 * Cart Recovery Exports Loader.
 *
 * Registers Cart Recovery exporters and handles export form rendering.
 *
 * @package     EDD\Pro\CartRecovery\Admin\Exports
 * @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\Exports;

use EDD\EventManagement\SubscriberInterface;

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

/**
 * Cart Recovery Exports Loader Class.
 *
 * Registers Cart Recovery exporters and handles export form rendering.
 *
 * @since 3.6.5
 */
class Loader implements SubscriberInterface {
	use \EDD\Pro\CartRecovery\Traits\Status;

	/**
	 * Returns an array of events that this subscriber wants to listen to.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public static function get_subscribed_events() {
		return array(
			'edd_export_init' => 'register_exporters',
			'edd_export_form' => 'render_export_form',
		);
	}

	/**
	 * Register Cart Recovery exporters with EDD.
	 *
	 * @since 3.6.5
	 * @param \EDD\Admin\Exports\Registry $registry Export registry instance.
	 */
	public function register_exporters( $registry ) {
		try {
			$registry->register_exporter(
				'acr_email_logs',
				array(
					'label'       => __( 'Cart Recovery Email Logs', 'easy-digital-downloads' ),
					'description' => __( 'Download a CSV of abandoned cart recovery email logs including delivery status, engagement metrics, and recipient information.', 'easy-digital-downloads' ),
					'class'       => Exporters\EmailLogs::class,
					'priority'    => 70,
				)
			);
		} catch ( \Exception $e ) {
			error_log( '[EDD ACR] Failed to register email logs exporter: ' . $e->getMessage() );
		}
	}

	/**
	 * Render the export form fields for Cart Recovery exports.
	 *
	 * @since 3.6.5
	 * @param string $exporter_id The exporter ID.
	 */
	public function render_export_form( $exporter_id ) {
		// Only render for our exporter.
		if ( 'acr_email_logs' !== $exporter_id ) {
			return;
		}

		?>
		<label for="edd-acr-status" class="screen-reader-text">
			<?php esc_html_e( 'Select Status', 'easy-digital-downloads' ); ?>
		</label>
		<?php
		$status_select = new \EDD\HTML\Select(
			array(
				'id'                => 'edd-acr-status',
				'name'              => 'status',
				'show_option_empty' => __( 'All Statuses', 'easy-digital-downloads' ),
				'show_option_all'   => false,
				'show_option_none'  => false,
				'options'           => $this->get_email_statuses(),
			)
		);
		$status_select->output();

		$date_range = new \EDD\HTML\FromTo(
			array(
				'legend' => __( 'Email Logs Export Date Range', 'easy-digital-downloads' ),
				'id'     => 'email-logs-export',
			)
		);
		$date_range->output();
	}
}
