<?php

declare(strict_types=1);

namespace BotGuard\Services\Detection\Modules;

use BotGuard\Services\Detection\ModuleResult;
use BotGuard\Services\Detection\SignalBag;

/**
 * Cookie / storage behaviour module.
 *
 * Verifies that the client accepted a probe cookie, persisted it across the
 * challenge round-trip, and exposes localStorage/sessionStorage. Many simple
 * bots and hardened headless configs disable cookies or storage.
 */
final class CookieModule extends AbstractModule
{
    public function key(): string
    {
        return 'cookies';
    }

    public function analyze(SignalBag $signals): ModuleResult
    {
        $cookies = $signals->clientArray('cookies');

        if ($cookies === []) {
            return $this->scored(0.2, [], ['No cookie/storage evidence'], ['present' => false]);
        }

        $enabled = (bool) ($cookies['enabled'] ?? false);
        $persisted = (bool) ($cookies['persisted'] ?? false);
        $localStorage = (bool) ($cookies['local_storage'] ?? false);
        $sessionStorage = (bool) ($cookies['session_storage'] ?? false);

        $this->detail('cookies', $cookies);

        $this->check('Cookies enabled', $enabled, 2.0);
        $this->check('Cookie persisted across round-trip', $persisted, 2.0);
        $this->check('localStorage available', $localStorage, 1.0);
        $this->check('sessionStorage available', $sessionStorage, 1.0);

        return $this->result();
    }
}
