<?php
/**
 * Plugin settings helpers.
 *
 * @package Live_Traffic_Analytics
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides shared settings defaults and sanitization.
 */
class WPLTA_Settings {

	const OPTION_DASHBOARD_REFRESH = 'wplta_dashboard_refresh_ms';
	const OPTION_TRACKING_HEARTBEAT = 'wplta_tracking_heartbeat_ms';
	const ONBOARDING_COUPON_CODE = 'WELCOME100';

	const DEFAULT_DASHBOARD_REFRESH = 10000;
	const DEFAULT_TRACKING_HEARTBEAT = 60000;

	/**
	 * Get the onboarding offer coupon code.
	 *
	 * @return string
	 */
	public static function get_onboarding_coupon() {
		return self::ONBOARDING_COUPON_CODE;
	}

	/**
	 * Return selectable refresh intervals.
	 *
	 * @param int $default_ms Unused; kept for backward compatibility.
	 * @return array
	 */
	public static function get_interval_options( $default_ms = 0 ) {
		$options = array(
			10 * 1000                     => __( '10 seconds', 'live-traffic-analytics' ),
			MINUTE_IN_SECONDS * 1000      => __( '1 minute', 'live-traffic-analytics' ),
			10 * MINUTE_IN_SECONDS * 1000 => __( '10 minutes', 'live-traffic-analytics' ),
			HOUR_IN_SECONDS * 1000        => __( '1 hour', 'live-traffic-analytics' ),
			12 * HOUR_IN_SECONDS * 1000   => __( '12 hours', 'live-traffic-analytics' ),
			DAY_IN_SECONDS * 1000         => __( '24 hours', 'live-traffic-analytics' ),
			7 * DAY_IN_SECONDS * 1000     => __( '7 days', 'live-traffic-analytics' ),
		);

		return $options;
	}

	/**
	 * Sanitize a selected interval against the allowed values.
	 *
	 * @param mixed $value Selected value.
	 * @param int   $default_ms Default fallback value.
	 * @return int
	 */
	public static function sanitize_interval( $value, $default_ms ) {
		$value   = absint( $value );
		$options = self::get_interval_options( $default_ms );

		return array_key_exists( $value, $options ) ? $value : (int) $default_ms;
	}

	/**
	 * Get dashboard refresh interval in milliseconds.
	 *
	 * @return int
	 */
	public static function get_dashboard_refresh_ms() {
		return self::sanitize_interval(
			get_option( self::OPTION_DASHBOARD_REFRESH, self::DEFAULT_DASHBOARD_REFRESH ),
			self::DEFAULT_DASHBOARD_REFRESH
		);
	}

	/**
	 * Get public tracking heartbeat interval in milliseconds.
	 *
	 * @return int
	 */
	public static function get_tracking_heartbeat_ms() {
		return self::sanitize_interval(
			get_option( self::OPTION_TRACKING_HEARTBEAT, self::DEFAULT_TRACKING_HEARTBEAT ),
			self::DEFAULT_TRACKING_HEARTBEAT
		);
	}

	/**
	 * Get the live activity lookback window.
	 *
	 * @return int
	 */
	public static function get_active_window_seconds() {
		$heartbeat_seconds = (int) ceil( self::get_tracking_heartbeat_ms() / 1000 );

		return max( 5 * MINUTE_IN_SECONDS, $heartbeat_seconds + MINUTE_IN_SECONDS );
	}

	/**
	 * Get active window dashboard hint.
	 *
	 * @return string
	 */
	public static function get_active_window_hint() {
		return sprintf(
			/* translators: %s: active user lookback interval. */
			__( 'Seen in the last %s', 'live-traffic-analytics' ),
			self::format_interval_label( self::get_active_window_seconds() * 1000 )
		);
	}

	/**
	 * Format an interval for admin display.
	 *
	 * @param int $milliseconds Interval in milliseconds.
	 * @return string
	 */
	public static function format_interval_label( $milliseconds ) {
		$seconds = max( 1, (int) round( $milliseconds / 1000 ) );

		if ( $seconds < MINUTE_IN_SECONDS ) {
			return sprintf(
				/* translators: %d: number of seconds. */
				_n( '%d second', '%d seconds', $seconds, 'live-traffic-analytics' ),
				$seconds
			);
		}

		if ( 0 === $seconds % DAY_IN_SECONDS ) {
			$days = (int) ( $seconds / DAY_IN_SECONDS );

			// Show a single day as "24 hours" to match the selectable option label.
			if ( 1 === $days ) {
				return __( '24 hours', 'live-traffic-analytics' );
			}

			return sprintf(
				/* translators: %d: number of days. */
				_n( '%d day', '%d days', $days, 'live-traffic-analytics' ),
				$days
			);
		}

		if ( 0 === $seconds % HOUR_IN_SECONDS ) {
			$hours = (int) ( $seconds / HOUR_IN_SECONDS );
			return sprintf(
				/* translators: %d: number of hours. */
				_n( '%d hour', '%d hours', $hours, 'live-traffic-analytics' ),
				$hours
			);
		}

		$minutes = (int) round( $seconds / MINUTE_IN_SECONDS );
		return sprintf(
			/* translators: %d: number of minutes. */
			_n( '%d minute', '%d minutes', $minutes, 'live-traffic-analytics' ),
			$minutes
		);
	}
}
