<?php

use Cleantalk\ApbctWP\Variables\AltSessions;
use PHPUnit\Framework\TestCase;

/**
 * These tests are auto generated by GitHub Copilot
 */
class TestBotDetectorLogGathering extends TestCase
{
    public function test_returnsFrontendDataLog()
    {
        global $apbct;
        $apbct->settings['data__bot_detector_enabled'] = '1';
        AltSessions::set('ct_bot_detector_frontend_data_log', json_encode(['log' => 'data']));

        $result = apbct__bot_detector_get_fd_log();
        $expected = json_encode([
                                    'plugin_status' => 'OK',
                                    'error_msg' => '',
                                    'frontend_data_log' => ['log' => 'data']
                                ]);

        $this->assertEquals($expected, $result);
    }

    public function test_returnsErrorWhenDisabled()
    {
        global $apbct;
        $apbct->settings['data__bot_detector_enabled'] = '0';

        $result = apbct__bot_detector_get_fd_log();
        $expected = json_encode([
                                    'plugin_status' => 'ERROR',
                                    'error_msg' => 'bot detector library usage is disabled',
                                    'frontend_data_log' => ''
                                ]);

        $this->assertEquals($expected, $result);
    }

    public function test_returnsErrorWhenNoLogFound()
    {
        global $apbct;
        $apbct->settings['data__bot_detector_enabled'] = '1';
        AltSessions::wipe();

        $result = apbct__bot_detector_get_fd_log();
        $expected = json_encode([
                                    'plugin_status' => 'ERROR',
                                    'error_msg' => 'no log found in alt sessions',
                                    'frontend_data_log' => ''
                                ]);

        $this->assertEquals($expected, $result);
    }

    public function test_returnsErrorWhenLogNotString()
    {
        global $apbct;
        $apbct->settings['data__bot_detector_enabled'] = '1';
        AltSessions::set('ct_bot_detector_frontend_data_log', false);

        $result = apbct__bot_detector_get_fd_log();
        $expected = json_encode([
                                    'plugin_status' => 'ERROR',
                                    'error_msg' => 'no log found in alt sessions',
                                    'frontend_data_log' => ''
                                ]);

        $this->assertEquals($expected, $result);
    }

    public function test_returnsErrorWhenLogNotJson()
    {
        global $apbct;
        $apbct->settings['data__bot_detector_enabled'] = '1';
        AltSessions::set('ct_bot_detector_frontend_data_log', 'invalid json');

        $result = apbct__bot_detector_get_fd_log();
        $expected = json_encode([
                                    'plugin_status' => 'ERROR',
                                    'error_msg' => 'can not decode data from alt sessions',
                                    'frontend_data_log' => ''
                                ]);

        $this->assertEquals($expected, $result);
    }
}
