<?php
/**
 * StatisticsData AJAX handler
 *
 * Returns JSON statistics data for Chart.js and DataTables on the stats page.
 *
 * @package Relatedly\Admin\Ajax
 * @since   2.0.0
 * @since   2.3.0 Added breakdown, top sources, and daily-by-type payloads.
 */

namespace Relatedly\Admin\Ajax;

use Relatedly\Core\Cache;
use Relatedly\Admin\Statistics\DataProvider;
use Relatedly\Premium\FeatureGate;

defined( 'ABSPATH' ) || exit;

/**
 * Returns dashboard statistics data over AJAX.
 *
 * @since 2.0.0
 * @since 2.3.0 Added top_sources and daily_by_type responses.
 */
class StatisticsData {

	/**
	 * Statistics data provider.
	 *
	 * @since 2.0.0
	 * @var DataProvider
	 */
	private DataProvider $data;

	/**
	 * Create the statistics AJAX handler.
	 *
	 * @since 2.0.0
	 * @param Cache $cache Cache service.
	 */
	public function __construct( Cache $cache ) {
		$this->data = new DataProvider( $cache );
	}

	/**
	 * Register the statistics AJAX action.
	 *
	 * @since 2.0.0
	 */
	public function init_hooks(): void {
		add_action( 'wp_ajax_relatedly_get_statistics', [ $this, 'handle' ] );
	}

	/**
	 * Return the requested statistics payload.
	 *
	 * @since 2.0.0
	 * @since 2.3.0 Dispatch by requested dataset type.
	 */
	public function handle(): void {
		check_ajax_referer( 'relatedly_statistics', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( [ 'message' => __( 'Permission denied.', 'relatedly' ) ] );
		}

		if ( ! FeatureGate::can_use_statistics_dashboard() ) {
			wp_send_json_error( [ 'message' => __( 'Advanced statistics are available with a paid plan.', 'relatedly' ) ] );
		}

		$start = sanitize_text_field( wp_unslash( $_POST['start_date'] ?? gmdate( 'Y-m-d', strtotime( '-30 days' ) ) ) );
		$end   = sanitize_text_field( wp_unslash( $_POST['end_date'] ?? gmdate( 'Y-m-d' ) ) );
		$type  = sanitize_text_field( wp_unslash( $_POST['dataset'] ?? 'summary' ) );

		$payload = $this->get_dataset( $type, $start, $end );

		wp_send_json_success( $payload );
	}

	/**
	 * Build the requested dataset.
	 *
	 * @since 2.3.0
	 * @param string $type       Dataset identifier.
	 * @param string $start_date Y-m-d.
	 * @param string $end_date   Y-m-d.
	 * @return array
	 */
	private function get_dataset( string $type, string $start_date, string $end_date ): array {
		switch ( $type ) {
			case 'daily':
				return [
					'daily'         => $this->data->get_daily_counts( $start_date, $end_date ),
					'daily_by_type' => $this->data->get_daily_counts_by_display_type( $start_date, $end_date ),
				];

			case 'top_clicked':
				return [
					'top_posts' => $this->data->get_top_clicked( $start_date, $end_date, 20 ),
				];

			case 'top_sources':
				return [
					'top_sources' => $this->data->get_top_sources( $start_date, $end_date, 20 ),
				];

			case 'summary':
			default:
				return [
					'summary'     => $this->data->get_summary( $start_date, $end_date ),
					'daily'       => $this->data->get_daily_counts( $start_date, $end_date ),
					'top_posts'   => $this->data->get_top_clicked( $start_date, $end_date, 20 ),
					'top_sources' => $this->data->get_top_sources( $start_date, $end_date, 20 ),
				];
		}
	}
}
