<?php

defined( 'ABSPATH' ) || exit;

class MonsterInsights_Revenue_Widget extends MonsterInsights_Abstract_Widget {

	public function __construct() {
		$this->type = 'revenue';
		$this->title = __( 'Revenue', 'google-analytics-premium' );
		$this->category = __( 'Key Metrics', 'google-analytics-premium' );
		$this->icon = 'dashicons-money-alt'; // WordPress Dashicon
		$this->support = array(
			'metrics' => array( 'totalRevenue' ), // Single metric - no user customization
			'dimensions' => array(
				'line-chart' => array( 'date' ),
			),
			'types' => array( 'infobox', 'line-chart' ),
		);
		$this->defaults = array(
			'metrics' => array( 'totalRevenue' ),
			'type' => 'infobox',
			'compare' => true,
		);
	}

	/**
	 * Build API query for this widget based on configuration.
	 *
	 * @param array $config Widget configuration from the frontend.
	 * @return array API query parameters.
	 */
	public function build_api_query( $config ) {
		$query = parent::build_api_query( $config );

		// Determine the display type
		$display_type = ! empty( $config['displayType'] ) ? $config['displayType'] : $this->defaults['type'];

		// Set dimensions based on display type
		if ( 'line-chart' === $display_type ) {
			// Line chart needs date dimension for time series
			$query['dimensions'] = array( 'date' );
		} else {
			// Infobox: no dimensions, just total
			$query['dimensions'] = array();
		}

		return $query;
	}
}
