<?php
/**
 * Campaign Tracker Report.
 *
 * @package     EDD\Pro\CampaignTracker\Admin\Reports
 * @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\Reports;

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

/**
 * Campaign Tracker Report Class.
 *
 * @since 3.6.3
 */
class CampaignTracker {

	/**
	 * The reports registry.
	 *
	 * @var \EDD\Reports\Data\Report_Registry
	 */
	private $reports;

	/**
	 * The report ID.
	 *
	 * @var string
	 */
	private $id = 'campaign_tracker';

	/**
	 * Registers the campaign tracker reports.
	 *
	 * @since 3.6.3
	 * @param \EDD\Reports\Data\Report_Registry $reports The reports registry.
	 * @return void
	 */
	public function __construct( $reports ) {
		$this->reports = $reports;

		try {
			$this->register_report();
		} catch ( \Exception $e ) {
			error_log( 'Campaign Tracker Report Registration Error: ' . $e->getMessage() );
		}
	}

	/**
	 * Registers the campaign tracker top level report.
	 *
	 * @since 3.6.3
	 * @return void
	 */
	public function register_report() {
		$tiles   = $this->get_tiles();
		$tables  = $this->get_tables();
		$filters = $this->get_filters();

		$this->reports->add_report(
			$this->id,
			array(
				'label'     => __( 'Campaigns', 'easy-digital-downloads' ),
				'icon'      => 'megaphone',
				'priority'  => 50,
				'filters'   => $filters,
				'endpoints' => array(
					'tiles'  => array_keys( $tiles ),
					'tables' => array_keys( $tables ),
				),
			)
		);

		// Register tiles.
		foreach ( $tiles as $tile => $class ) {
			new $class( $this->reports );
		}

		// Register tables.
		foreach ( $tables as $table => $class ) {
			new $class( $this->reports );
		}
	}

	/**
	 * Returns the tiles for the campaign tracker report.
	 *
	 * @since 3.6.3
	 * @return array
	 */
	private function get_tiles(): array {
		// Check if a specific campaign filter is applied via URL parameter.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$has_filter = isset( $_GET['campaign_tracker'] );
		$campaign   = \EDD\Reports\get_filter_value( 'campaign_tracker' );

		// If a campaign filter is explicitly set in URL and it's not 'all', show campaign-specific tiles.
		if ( $has_filter && 'all' !== $campaign ) {
			return array(
				'campaign_tracker_earnings' => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\CampaignEarnings',
				'campaign_tracker_sales'    => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\CampaignSales',
				'top_source_revenue'        => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\TopSourceRevenue',
				'top_medium_revenue'        => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\TopMediumRevenue',
			);
		}

		// Otherwise, show overview tiles for all campaigns.
		return array(
			'top_campaign_orders'  => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\TopCampaignOrders',
			'top_campaign_revenue' => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\TopCampaignRevenue',
			'top_campaign_aov'     => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tiles\\TopCampaignAOV',
		);
	}

	/**
	 * Gets the tables for the campaign tracker report.
	 *
	 * @since 3.6.3
	 * @return array
	 */
	private function get_tables(): array {
		return array(
			'campaign_sources' => 'EDD\\Pro\\CampaignTracker\\Admin\\Reports\\Endpoints\\Tables\\CampaignSources',
		);
	}

	/**
	 * Gets the filters for the campaign tracker report.
	 *
	 * @since 3.6.3
	 * @return array
	 */
	private function get_filters(): array {
		return array(
			'dates',
			'campaign_tracker',
		);
	}
}
