<?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 -> code compliance: we put useful comments where needed.. */
class HappyFenceNGBStore extends HappyFenceNGAStore {

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

			parent::aInit(
				HappyFenceNGStStored::get_b_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_b_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_b_store_table_name(), $module, $prop, $value );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $module ... the name.
	 */
	private static function deleteNullTokens( string $module ): bool {
		try {
			$a_module = self::mov( $module );
			$table    = HappyFenceNGStStored::get_b_store_table_name();
			$deleted  = HAPPYFENCENG_DB->query(
				HAPPYFENCENG_DB->prepare(
					"DELETE FROM {$table} WHERE module = %s AND value IS NULL",
					$a_module
				)
			);
			if ( $deleted > 0 ) {
				return true;
			} else {
				return false;
			}
		} catch ( Throwable $t ) {
			return false;
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $next ... time I will choose java.
	 * @param string $module ... the name, what else.
	 * @param string $token .. is a token, what else.
	 */
	public static function burn( string $next, string $module, string $token ): string {
		try {
			HAPPYFENCENG_DB->query( 'START TRANSACTION' );
			$my_data = maybe_unserialize( self::get( $module, $token ) );
			if ( $my_data && is_array( $my_data ) ) {
				$my_data = null;
				self::set( $module, $token, $my_data );
				HAPPYFENCENG_DB->query( 'COMMIT' );
				return $next;
			} else {
				return HappyFenceNGPlaceholder::KO;
			}
		} catch ( Throwable $t ) {
			HAPPYFENCENG_DB->query( 'ROLLBACK' );
			return HappyFenceNGPlaceholder::KO;
		}
	}

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