<?php
/**
 * Dashboard Widget
 *
 * Adds a Relatedly summary widget to the WordPress dashboard.
 * Stats are cached for 10 minutes to avoid expensive queries on every load.
 *
 * @package Relatedly\Admin\Dashboard
 * @since   2.0.0
 */

namespace Relatedly\Admin\Dashboard;

use Relatedly\Core\Options;
use Relatedly\Core\Cache;
use Relatedly\Admin\Statistics\DataProvider;

defined( 'ABSPATH' ) || exit;

/**
 * Renders a Relatedly summary widget on the WordPress dashboard.
 *
 * @since 2.0.0
 */
class Widget {

	/**
	 * Plugin settings repository.
	 *
	 * @since 2.0.0
	 * @var Options
	 */
	private Options $options;

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

	/**
	 * Create the dashboard widget.
	 *
	 * @since 2.0.0
	 * @param Options $options Plugin settings repository.
	 * @param Cache   $cache   Cache service.
	 */
	public function __construct( Options $options, Cache $cache ) {
		$this->options = $options;
		$this->data    = new DataProvider( $cache );
	}

	/**
	 * Register dashboard hooks.
	 *
	 * @since 2.0.0
	 */
	public function init_hooks(): void {
		add_action( 'wp_dashboard_setup', array( $this, 'register' ) );
	}

	/**
	 * Register the dashboard widget.
	 *
	 * @since 2.0.0
	 */
	public function register(): void {
		wp_add_dashboard_widget(
			'relatedly_dashboard_widget',
			__( 'Relatedly — Related Posts', 'relatedly' ),
			array( $this, 'render' )
		);
	}

	/**
	 * Render the dashboard widget markup.
	 *
	 * @since 2.0.0
	 */
	public function render(): void {

		if ( ! $this->options->get( 'enable_click_tracking', true ) ) {
			printf(
				'<p>%s</p>',
				esc_html__( 'Stats are turned off. Enable them in Relatedly Settings to see reports here.', 'relatedly' )
			);
			return;
		}
		$end        = gmdate( 'Y-m-d' );
		$start_7    = gmdate( 'Y-m-d', strtotime( '-7 days' ) );
		$start_30   = gmdate( 'Y-m-d', strtotime( '-30 days' ) );
		$summary_7  = $this->data->get_summary( $start_7, $end );
		$summary_30 = $this->data->get_summary( $start_30, $end );
		?>
		<table class="widefat striped relatedly-dashboard-stats-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Period', 'relatedly' ); ?></th>
					<th><?php esc_html_e( 'Clicks', 'relatedly' ); ?></th>
					<th><?php esc_html_e( 'Views', 'relatedly' ); ?></th>
					<th><?php esc_html_e( 'Click Rate', 'relatedly' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><?php esc_html_e( 'Last 7 days', 'relatedly' ); ?></td>
					<td><?php echo esc_html( number_format_i18n( $summary_7['total_clicks'] ) ); ?></td>
					<td><?php echo esc_html( number_format_i18n( $summary_7['total_impressions'] ) ); ?></td>
					<td><?php echo esc_html( $summary_7['ctr'] . '%' ); ?></td>
				</tr>
				<tr>
					<td><?php esc_html_e( 'Last 30 days', 'relatedly' ); ?></td>
					<td><?php echo esc_html( number_format_i18n( $summary_30['total_clicks'] ) ); ?></td>
					<td><?php echo esc_html( number_format_i18n( $summary_30['total_impressions'] ) ); ?></td>
					<td><?php echo esc_html( $summary_30['ctr'] . '%' ); ?></td>
				</tr>
			</tbody>
		</table>
		<p>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=relatedly-statistics' ) ); ?>">
				<?php esc_html_e( 'View full statistics →', 'relatedly' ); ?>
			</a>
		</p>
		<?php
	}
}
