<?php

declare(strict_types=1);

namespace Tests\Unit;

use InvalidArgumentException;
use Tests\Support\DatabaseSeeder;
use Tests\Support\QdbTestCase;

final class SettingsTest extends QdbTestCase
{
    public function testDefaults(): void
    {
        require_once dirname(__DIR__, 2) . '/lib/settings.php';
        $d = qdb_settings_defaults();
        $this->assertSame(10, $d['login_max_attempts']);
        $this->assertSame(30 * 24 * 60 * 60, $d['session_ttl_seconds']);
    }

    public function testValidateRejectsOutOfRange(): void
    {
        require_once dirname(__DIR__, 2) . '/lib/settings.php';
        $this->expectException(InvalidArgumentException::class);
        qdb_settings_validate_and_merge(['login_max_attempts' => 0]);
    }

    public function testSaveAndLoadRoundTrip(): void
    {
        require_once dirname(__DIR__, 2) . '/lib/settings.php';
        [$pdo, $tmpDb, $lockFp] = qdb_open(true);
        $custom = qdb_settings_validate_and_merge([
            'login_max_attempts'    => 5,
            'login_window_seconds'  => 600,
            'login_lockout_seconds' => 1200,
            'session_ttl_seconds'   => 86400,
            'temp_session_ttl_seconds' => 900,
        ]);
        qdb_settings_save_on_pdo($pdo, $custom);
        qdb_save($tmpDb, $lockFp);
        [$pdo2, $tmp2, $lock2] = qdb_open(false);
        $loaded = qdb_settings_get_on_pdo($pdo2);
        qdb_discard($tmp2, $lock2);
        $this->assertSame(5, $loaded['login_max_attempts']);
        $this->assertSame(86400, $loaded['session_ttl_seconds']);
    }

    public function testSessionTtlHelper(): void
    {
        require_once dirname(__DIR__, 2) . '/lib/settings.php';
        $this->assertSame(600, qdb_session_ttl_seconds(['temp_session_ttl_seconds' => 600], true));
        $this->assertSame(3600, qdb_session_ttl_seconds(['session_ttl_seconds' => 3600], false));
    }
}
