<?php

namespace MintyPHP\Tests\Service\Settings;

use MintyPHP\Repository\Settings\SettingRepositoryInterface;
use MintyPHP\Service\Settings\SettingsFrontendTelemetryGateway;
use MintyPHP\Service\Settings\SettingsMetadataGateway;
use PHPUnit\Framework\TestCase;

class SettingsFrontendTelemetryGatewayTest extends TestCase
{
    public function testDefaultsAreUsedWhenSettingsMissing(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->method('getValue')->willReturn(null);

        $gateway = new SettingsFrontendTelemetryGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->isFrontendTelemetryEnabled());
        $this->assertSame(0.2, $gateway->getFrontendTelemetrySampleRate());
        $this->assertSame(['warn_once', 'ajax_error'], $gateway->getFrontendTelemetryAllowedEvents());
    }

    public function testSetSampleRateRejectsOutOfRangeValues(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->expects($this->never())->method('set');

        $gateway = new SettingsFrontendTelemetryGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->setFrontendTelemetrySampleRate(1.5));
    }

    public function testSetAllowedEventsNormalizesValues(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->expects($this->once())
            ->method('set')
            ->with(
                'frontend_telemetry_allowed_events',
                'ajax_error,warn_once',
                'setting.frontend_telemetry_allowed_events'
            )
            ->willReturn(true);

        $gateway = new SettingsFrontendTelemetryGateway(new SettingsMetadataGateway($settings));
        $this->assertTrue($gateway->setFrontendTelemetryAllowedEvents(['frontend.warn_once', 'ajax_error', 'invalid']));
    }
}
