<?php

namespace MintyPHP\Tests\Service\Settings;

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

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->isMicrosoftAutoRememberDefault());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertTrue($gateway->isMicrosoftAutoRememberDefault());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(30, $gateway->getRememberTokenLifetimeDays());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(90, $gateway->getRememberTokenLifetimeDays());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(30, $gateway->getRememberTokenLifetimeDays());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(30, $gateway->getRememberTokenLifetimeDays());
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertFalse($gateway->setRememberTokenLifetimeDays(0));
    }

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

        $gateway = new SettingsLoginPersistenceGateway(new SettingsMetadataGateway($settings));
        $this->assertSame(604800, $gateway->getRememberTokenLifetimeSeconds());
    }
}
