<?php
/**
 * Abstract anti-spam provider class.
 *
 * @package Awesomemotive\Rewardswp\Abstracts
 * @since 1.2.0
 */

namespace Awesomemotive\Rewardswp\Abstracts;

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

use Awesomemotive\Rewardswp\Interfaces\AntiSpamProviderInterface;
use function Awesomemotive\Rewardswp\Helpers\can_access_rewardswp_feature;

/**
 * Abstract class for anti-spam providers.
 *
 * Provides common functionality shared between all anti-spam providers.
 *
 * @since 1.2.0
 */
abstract class AbstractAntiSpamProvider implements AntiSpamProviderInterface {
	/**
	 * The error message when validation fails.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	protected string $error_message = '';

	/**
	 * Check if the provider is available for use.
	 *
	 * Checks both license access and configuration status.
	 *
	 * @since 1.2.0
	 *
	 * @return bool True if available, false otherwise.
	 */
	public function is_available(): bool {
		return can_access_rewardswp_feature( $this->get_feature_slug() ) && $this->is_configured();
	}

	/**
	 * Get the error message when validation fails.
	 *
	 * Returns a generic message to avoid revealing anti-spam mechanism details.
	 *
	 * @since 1.2.0
	 *
	 * @return string The error message.
	 */
	public function get_error_message(): string {
		if ( empty( $this->error_message ) ) {
			return __( 'Something went wrong. Please try again.', 'rewardswp' );
		}

		return $this->error_message;
	}

	/**
	 * Set the error message.
	 *
	 * @since 1.2.0
	 *
	 * @param string $message The error message.
	 *
	 * @return void
	 */
	protected function set_error( string $message ): void {
		$this->error_message = $message;
	}
}

