<?php
/**
 * Campaign Earnings Tile.
 *
 * @package     EDD\Pro\CampaignTracker\Admin\Reports\Endpoints\Tiles
 * @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\Endpoints\Tiles;

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

/**
 * Campaign Earnings Tile.
 *
 * @since 3.6.3
 */
class CampaignEarnings extends CampaignTile {

	/**
	 * Gets the tile endpoint ID.
	 *
	 * @since 3.6.3
	 * @return string
	 */
	protected function get_id(): string {
		return 'campaign_tracker_earnings';
	}

	/**
	 * Gets the tile label for display.
	 *
	 * @since 3.6.3
	 * @return string
	 */
	protected function get_label(): string {
		return __( 'Earnings', 'easy-digital-downloads' );
	}

	/**
	 * Gets the data for the endpoint.
	 *
	 * @since 3.6.3
	 * @return string
	 */
	protected function get_data() {
		global $wpdb;

		$column = $this->exclude_taxes ? 'o.total - o.tax' : 'o.total';

		$earnings = $wpdb->get_var(
			"SELECT SUM({$column})
			{$this->get_from_clause()}
			{$this->get_base_where_clause()}"
		);

		if ( is_null( $earnings ) ) {
			$earnings = 0;
		}

		return edd_currency_filter( edd_format_amount( $earnings ) );
	}
}
