<?php
/**
 * LinkCentral Total Clicks Class
 *
 * This class handles the functionality for retrieving and displaying
 * total click statistics for all links and specific links in the plugin.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class LinkCentral_Total_Clicks {

	/**
	 * The main admin object.
	 *
	 * @var LinkCentral_Admin
	 */
	private $admin;

	/**
	 * Constructor.
	 *
	 * @param LinkCentral_Admin $admin The main admin object.
	 */
	public function __construct( $admin ) {
		$this->admin = $admin;
	}

	/**
	 * Get click statistics for the given filter context.
	 *
	 * @param LinkCentral_Insights_Filters $filters Filter context.
	 * @return array
	 */
	public function get_click_data_for_filters( LinkCentral_Insights_Filters $filters ) {
		if ( LinkCentral_Insights_Query_Builder::category_filter_is_empty( $filters ) ) {
			return array(
				'labels'        => array(),
				'clicks'        => array(),
				'unique_clicks' => array(),
			);
		}

		$date_range = LinkCentral_Insights_Query_Builder::resolve_date_range( $filters );

		return $this->get_click_data( $date_range, $filters );
	}

	/**
	 * Get click statistics data.
	 *
	 * @param array                        $date_range Start and end DateTime objects (for chart axis fill).
	 * @param LinkCentral_Insights_Filters $filters    Filter context (date, link, category, drill-down).
	 * @return array
	 */
	private function get_click_data( $date_range, LinkCentral_Insights_Filters $filters ) {
		global $wpdb;

		list( $start, $end ) = $date_range;
		$track_unique        = get_option( 'linkcentral_track_unique_visitors', false );

		$clauses = array();
		$params  = array();

		if ( ! LinkCentral_Insights_Query_Builder::append_stats_filters( $filters, $clauses, $params ) ) {
			return array(
				'labels'        => array(),
				'clicks'        => array(),
				'unique_clicks' => array(),
			);
		}

		$query  = 'SELECT DATE(s.click_date) as date, COUNT(*) as clicks';
		if ( $track_unique ) {
			$query .= ', SUM(s.first_click) as unique_clicks';
		}
		$query .= " FROM {$wpdb->prefix}linkcentral_stats s";
		$query .= ' WHERE ' . implode( ' AND ', $clauses );
		$query .= ' GROUP BY DATE(s.click_date)';

		$results = LinkCentral_Insights_Query_Builder::query( $query, $params );

		$data = array(
			'labels'        => array(),
			'clicks'        => array(),
			'unique_clicks' => array(),
		);

		$click_data        = array();
		$unique_click_data = array();
		foreach ( $results as $row ) {
			$click_data[ $row['date'] ] = (int) $row['clicks'];
			if ( $track_unique ) {
				$unique_click_data[ $row['date'] ] = (int) $row['unique_clicks'];
			}
		}

		$interval   = new DateInterval( 'P1D' );
		$period     = new DatePeriod( $start, $interval, $end );
		foreach ( $period as $date ) {
			$date_string         = $date->format( 'Y-m-d' );
			$data['labels'][]    = $date->format( 'c' );
			$data['clicks'][]    = isset( $click_data[ $date_string ] ) ? $click_data[ $date_string ] : 0;
			$data['unique_clicks'][] = isset( $unique_click_data[ $date_string ] ) ? $unique_click_data[ $date_string ] : 0;
		}

		return $data;
	}
}
