<?php
/**
 * AbstractFormHandler for PromoTimeWidget module.
 *
 * Provides base configuration for all PromoTimeWidget form handlers,
 * including nonce and action used for data validation and processing.
 *
 * @package Ilabs\Firevert\Modules\PromoTimeWidget\Services\Form
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\PromoTimeWidget\Services\Form;

use Ilabs\Firevert\Core\Repository\Cache\CacheRepository;
use Ilabs\Firevert\Core\Form\FormHandler;
use Ilabs\Firevert\Modules\PromoTimeWidget\Services\Repository\PromoTimeWidgetRepository;

/**
 * Class AbstractFormHandler
 *
 * Defines common properties for form handlers used in the PromoTimeWidget module.
 */
abstract class AbstractFormHandler extends FormHandler {

	/**
	 * The name of the nonce field used for form submission verification.
	 *
	 * This field must be included in the form and match the $action
	 * to ensure the data comes from a legitimate source.
	 *
	 * @var string
	 */
	protected string $nonce = 'firevert_promo_time_widget_nonce';

	/**
	 * The action name used to verify the submitted nonce.
	 *
	 * Must match the string used when creating the nonce via wp_create_nonce().
	 *
	 * @var string
	 */
	protected string $action = 'firevert_promo_time_widget_save';

	/**
	 * Runs after saving PromoTimeWidget form data.
	 *
	 * Used to invalidate caches or perform post-save actions.
	 *
	 * @return void
	 */
	public function after_save(): void {
		if ( isset( $this->post ) ) {
			/**
			 * Get container DI.
			 *
			 * @var CacheRepository $cache
			 */
			$cache = firevert_container()->get( 'cache_repository' );
			$cache->flush_group( PromoTimeWidgetRepository::PROMO_TIME_WIDGET_CACHE_GROUP );
		}
	}
}
