<?php
/**
 * SidebarBoxes
 *
 * Renders the review request and help boxes
 * shown in the settings page sidebar.
 *
 * @package Relatedly\Admin
 * @since   2.0.0
 */

namespace Relatedly\Admin;

use Relatedly\Core\Options;
use Relatedly\Premium\FeatureGate;

defined( 'ABSPATH' ) || exit;

/**
 * Renders optional boxes in the settings sidebar.
 *
 * @since 2.0.0
 */
class SidebarBoxes {

	/** Days after activation before showing the review request. */
	const REVIEW_DELAY_DAYS = 7;

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

	/**
	 * Create the sidebar boxes renderer.
	 *
	 * @since 2.0.0
	 * @param Options $options Plugin settings repository.
	 */
	public function __construct( Options $options ) {
		$this->options = $options;
	}

	/**
	 * Render all sidebar boxes.
	 *
	 * @since 2.0.0
	 */
	public function render(): void {
		$this->render_box( 'sidebar-review.php' );
		$this->render_box( 'sidebar-premium.php' );
		$this->render_box( 'sidebar-statistics.php' );
		$this->render_box( 'sidebar-help.php' );
	}

	/**
	 * Whether the review box should be shown.
	 *
	 * @since 2.0.0
	 */
	public function show_review_box(): bool {
		$dismissed = get_option( 'relatedly_review_dismissed', false );
		if ( 'permanent' === $dismissed ) {
			return false;
		}
		if ( is_numeric( $dismissed ) && time() < (int) $dismissed ) {
			return false;
		}

		$activated = (int) get_option( 'relatedly_activated_time', 0 );
		return $activated > 0 && ( time() - $activated ) >= ( self::REVIEW_DELAY_DAYS * DAY_IN_SECONDS );
	}

	/**
	 * Whether the premium box should be shown.
	 * Always shown to free users — cannot be permanently dismissed.
	 *
	 * @since 2.0.0
	 */
	public function show_premium_box(): bool {
		return ! FeatureGate::is_premium();
	}

	// -------------------------------------------------------------------------
	// Internal
	// -------------------------------------------------------------------------

	/**
	 * Render a sidebar box template when it exists.
	 *
	 * @param string $template Template filename.
	 */
	private function render_box( string $template ): void {
		$path = RELATEDLY_PLUGIN_DIR . 'templates/admin/' . $template;
		if ( file_exists( $path ) ) {
			$boxes = $this;
			include $path;
		}
	}
}
