<?php
/**
 * Clear Spam Log Action
 *
 * Handles the admin-post action that clears the Spam Protection diagnostic log.
 *
 * @package AmbassadorHub\Admin\Setup
 */

namespace AmbassadorHub\Admin\Setup;

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

use AmbassadorHub\Spam\SpamShield;

use function add_action;
use function admin_url;
use function check_admin_referer;
use function current_user_can;
use function wp_die;
use function wp_safe_redirect;

/**
 * Registers and handles the "Clear log" admin-post action on the Spam
 * Protection settings tab.
 */
final class ClearSpamLogAction {

	/**
	 * Register the admin-post hook.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'admin_post_ambh_clear_spam_log', array( self::class, 'handle' ) );
	}

	/**
	 * Handle the form submission.
	 *
	 * @return void
	 */
	public static function handle(): void {
		check_admin_referer( 'ambh_clear_spam_log', 'ambh_clear_spam_log_nonce' );

		if ( ! current_user_can( 'ambh_manage_hub' ) ) {
			wp_die( 'Insufficient permissions.' );
		}

		SpamShield::clear_log();

		wp_safe_redirect(
			admin_url( 'admin.php?page=ambassadorhub-settings&tab=spam' )
		);
		exit;
	}
}
