<?php
/**
 * @package Hafenstudios\Wellenbrecher
 */

namespace Hafenstudios\Wellenbrecher\Rules;

use Hafenstudios\Wellenbrecher\Engine\Context;
use Hafenstudios\Wellenbrecher\Engine\RuleResult;
use Hafenstudios\Wellenbrecher\Signals\TokenService;

defined( 'ABSPATH' ) || exit;

/**
 * Formular-Token fehlt oder ist keiner unserer Generationen zuordenbar.
 * In REST-/XML-RPC-Kontexten neutralisiert (meta['skip_token_rules']),
 * sonst 100 % False Positives auf legitime Clients.
 */
class TokenMissingRule extends AbstractRule {

	public const ID             = 'token_missing';
	public const DEFAULT_POINTS = 30;

	protected TokenService $tokens;

	public function __construct( TokenService $tokens, int $points = self::DEFAULT_POINTS ) {
		parent::__construct( $points );
		$this->tokens = $tokens;
	}

	public function id(): string {
		return self::ID;
	}

	public function stage(): int {
		return 1;
	}

	public function applies( Context $context ): bool {
		return empty( $context->meta['skip_token_rules'] );
	}

	public function evaluate( Context $context ): RuleResult {
		$status = $this->tokens->status( $context->signal( 'token' ) );
		return TokenService::STATUS_MISSING === $status ? $this->hit() : $this->miss();
	}
}
