<?php
/**
 * Campaign Exports Loader.
 *
 * @package     EDD\Pro\CampaignTracker\Admin\Exports
 * @copyright   Copyright (c) 2025, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.6.3
 */

namespace EDD\Pro\CampaignTracker\Admin\Exports;

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

use EDD\EventManagement\SubscriberInterface;

/**
 * Campaign Exports Loader Class.
 *
 * Registers campaign exporters and handles export form rendering.
 *
 * @since 3.6.3
 */
class Loader implements SubscriberInterface {

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

	/**
	 * Register campaign exporters with EDD.
	 *
	 * @since 3.6.3
	 * @param \EDD\Admin\Exports\Registry $registry Export registry instance.
	 */
	public function register_exporters( $registry ) {
		// Register Campaign Orders exporter.
		$registry->register_exporter(
			'campaign_orders',
			array(
				'label'       => __( 'Campaign Orders', 'easy-digital-downloads' ),
				'description' => __( 'Download a CSV of orders filtered by campaign. Use the filters to select a specific campaign and date range.', 'easy-digital-downloads' ),
				'class'       => CampaignOrders::class,
			)
		);

		// Register Campaign Customers exporter.
		$registry->register_exporter(
			'campaign_customers',
			array(
				'label'       => __( 'Campaign Customers', 'easy-digital-downloads' ),
				'description' => __( 'Download a CSV of customers with aggregated campaign purchase data. Shows total spent and order count per customer.', 'easy-digital-downloads' ),
				'class'       => CampaignCustomers::class,
			)
		);
	}

	/**
	 * Render the export form fields for campaign exports.
	 *
	 * @since 3.6.3
	 * @param string $exporter_id The exporter ID.
	 */
	public function render_export_form( $exporter_id ) {
		// Only render for our campaign exporters.
		if ( ! in_array( $exporter_id, array( 'campaign_orders', 'campaign_customers' ), true ) ) {
			return;
		}

		// Get unique campaign names from the database.
		$campaigns = $this->get_unique_campaign_names();

		?>
		<label for="edd-campaign-name" class="screen-reader-text">
			<?php esc_html_e( 'Select Campaign', 'easy-digital-downloads' ); ?>
		</label>
		<?php
		$campaign_select = new \EDD\HTML\Select(
			array(
				'id'                => 'edd-campaign-name',
				'name'              => 'campaign_name',
				'show_option_empty' => __( 'All Campaigns', 'easy-digital-downloads' ),
				'show_option_all'   => false,
				'show_option_none'  => false,
				'options'           => $campaigns,
				'chosen'            => true,
				'placeholder'       => __( 'Select a Campaign', 'easy-digital-downloads' ),
			)
		);
		$campaign_select->output();

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

	/**
	 * Get unique campaign names from order meta.
	 *
	 * @since 3.6.3
	 * @return array Array of campaign names (value => label format for dropdowns).
	 */
	private function get_unique_campaign_names() {
		global $wpdb;

		$campaigns = $wpdb->get_col(
			"SELECT DISTINCT meta_value
			 FROM {$wpdb->edd_ordermeta}
			 WHERE meta_key = '_eddct_campaign_name'
			 AND meta_value != ''
			 ORDER BY meta_value ASC"
		);

		if ( empty( $campaigns ) ) {
			return array();
		}

		return array_combine( $campaigns, $campaigns );
	}
}
