<?php
/**
 * Helpers for AccessLens options that must NOT autoload.
 *
 * update_option() only honors the autoload argument when the option is created;
 * on an existing option the stored autoload flag is left untouched on most WP
 * versions. To guarantee autoload=no on every write (options here can hold up
 * to ~100 violations, no reason to load them on every page request) we always
 * delete + add instead of update.
 *
 * @package AccessLens
 */

declare(strict_types=1);

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

/**
 * AccessLens_Options
 */
class AccessLens_Options {

	/**
	 * Set an option value, forcing autoload = 'no'.
	 *
	 * @param string $option Option name.
	 * @param mixed  $value  Value to store.
	 */
	public static function set( string $option, $value ): void {
		delete_option( $option );
		add_option( $option, $value, '', 'no' );
	}

	/**
	 * Get an option value.
	 *
	 * @param string $option  Option name.
	 * @param mixed  $default Default if not set.
	 * @return mixed
	 */
	public static function get( string $option, $default = false ) {
		return get_option( $option, $default );
	}

	/**
	 * Record the first-run timestamp once, if not already set.
	 */
	public static function maybe_set_installed_at(): void {
		if ( false === get_option( 'accesslens_installed_at', false ) ) {
			self::set( 'accesslens_installed_at', time() );
		}
	}

	/**
	 * Increment the completed-scan counter and return the new value.
	 */
	public static function increment_scan_count(): int {
		$count = absint( self::get( 'accesslens_scan_count', 0 ) ) + 1;
		self::set( 'accesslens_scan_count', $count );
		return $count;
	}
}
