<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

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

/**PHPCS -> this is the props database.. */
class HappyFenceNGCStore extends HappyFenceNGAStore {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function init() {

			parent::aInit(
				HappyFenceNGStStored::get_c_store_table_name(),
				' (
                    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                    module VARCHAR(128) NOT NULL,
                    prop   VARCHAR(128) NOT NULL,
                    value  LONGTEXT NOT NULL,
                    PRIMARY KEY (id),
                    UNIQUE KEY module_prop (module, prop)   
                ) '
			);
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $module ... the name.
	 * @param string $prop .. the name.
	 * @param string $adefault ... guess what?.
	 */
	public static function get( string $module, string $prop, $adefault = HappyFenceNGPlaceholder::NOT_INITIALIZED ) {
		return parent::fromEncryptedDb( HappyFenceNGStStored::get_c_store_table_name(), $module, $prop, $adefault );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $module ... the name.
	 * @param string $prop .. the name.
	 * @param string $value ... guess what?.
	 */
	public static function set( string $module, string $prop, $value ): void {
		parent::toEncryptedDb( HappyFenceNGStStored::get_c_store_table_name(), $module, $prop, $value );
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function uninstall() {
		HappyFenceNGLOGCORE::logcore(
			static::class . '->Requested uninstall for ->' . HappyFenceNGStStored::get_c_store_table_name(),
			HappyFenceNGSyslogFacility::DB,
			HappyFenceNGSyslogSeverity::INFO
		);
		parent::generic_uninstall( HappyFenceNGStStored::get_c_store_table_name() );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param HappyFenceNGRgs $func ... the name.
	 * @param HappyFenceNGRgs $status .. the status.
	 */
	public static function registerFunction( HappyFenceNGRgs $func, HappyFenceNGRgs $status ) {
		try {

			self::set(
				HappyFenceNGRgs::ID->value,
				$func->value,
				array(
					'status' => $status->value,
					'ts'     => time(),
				)
			);
		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				static::class . "->requested registerFunction $func->value, status $status->value, but an exception occurred " . $e->getMessage(),
				HappyFenceNGSyslogFacility::ZTV,
				HappyFenceNGSyslogSeverity::ERROR
			);
		}
	}


	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function getFunctions(): array {

		$all_functions = HappyFenceNGRgs::get_regs();
		$now           = time();
		$ten_minutes   = 600;
		$final         = HappyFenceNGPlaceholder::EMPTY_ARRAY;
		foreach ( $all_functions as $a_func ) {
			$raw = self::get( HappyFenceNGRgs::ID->value, $a_func->value, null );
			if ( HappyFenceNGPROC::not_equals( $raw, null ) ) {
				$is_recent = isset( $raw['ts'] ) && ( $now - $raw['ts'] ) <= $ten_minutes;
				$status    = $is_recent ? $raw['status'] : HappyFenceNGRgs::SO->value;
			} else {
				$status = HappyFenceNGRgs::NA->value;
			}

			$final[] = array(
				'module' => $a_func->value,
				'status' => $status,
			);
		}

		$priority = array(
			HappyFenceNGRgs::SR->value => 1,
			HappyFenceNGRgs::SO->value => 2,
			HappyFenceNGRgs::NA->value => 3,
			HappyFenceNGRgs::SP->value => 3,
			HappyFenceNGRgs::SA->value => 3,
			HappyFenceNGRgs::SE->value => 3,
		);

		usort(
			$final,
			function ( $a, $b ) use ( $priority ) {
				return $priority[ $a['status'] ] <=> $priority[ $b['status'] ];
			}
		);

		return $final;
	}
}
