<?php

namespace MintyPHP\Tests\Service\Settings;

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

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

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertTrue($gateway->isSystemAuditEnabled());
    }

    public function testIsSystemAuditEnabledReturnsFalseWhenDisabled(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
            return match ($key) {
                'system_audit_enabled' => '0',
                default => null,
            };
        });

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->isSystemAuditEnabled());
    }

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

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(365, $gateway->getSystemAuditRetentionDays());
    }

    public function testGetSystemAuditRetentionDaysReturnsValidValue(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
            return match ($key) {
                'system_audit_retention_days' => '180',
                default => null,
            };
        });

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(180, $gateway->getSystemAuditRetentionDays());
    }

    public function testGetSystemAuditRetentionDaysClampsToMinimum(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
            return match ($key) {
                'system_audit_retention_days' => '10',
                default => null,
            };
        });

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(365, $gateway->getSystemAuditRetentionDays());
    }

    public function testGetSystemAuditRetentionDaysClampsToMaximum(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->method('getValue')->willReturnCallback(static function (string $key): ?string {
            return match ($key) {
                'system_audit_retention_days' => '2000',
                default => null,
            };
        });

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(365, $gateway->getSystemAuditRetentionDays());
    }

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

        $gateway = new SettingsSystemAuditGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->setSystemAuditRetentionDays(10));
    }
}
