<?php
namespace boru\ai\Tests\Config;

use boru\ai\Config\Config;
use boru\ai\Config\ConfigResolver;
use PHPUnit\Framework\TestCase;

class ConfigResolverTest extends TestCase
{
    protected function tearDown(): void
    {
        putenv('AI_REPLAY_ENABLED');
        putenv('GEMINI_REPLAY_ENABLED');
        putenv('AI_STREAM_READ_SIZE');
        putenv('GEMINI_STREAM_READ_SIZE');
    }

    public function testReturnsProviderDefaultWhenNoOverridesExist()
    {
        $resolver = new ConfigResolver(new Config());

        $this->assertTrue($resolver->resolveProviderSetting('gemini', 'replay.enabled', true));
        $this->assertFalse($resolver->resolveProviderSetting('openai', 'replay.enabled', false));
    }

    public function testGlobalRuntimeSettingOverridesProviderDefault()
    {
        $config = new Config();
        $config->runtime(array(
            'replay' => array(
                'enabled' => true,
            ),
        ));

        $resolver = new ConfigResolver($config);

        $this->assertTrue($resolver->resolveProviderSetting('openai', 'replay.enabled', false));
    }

    public function testProviderSpecificSettingOverridesGlobalRuntimeSetting()
    {
        $config = new Config();
        $config->runtime(array(
            'replay' => array(
                'enabled' => false,
            ),
        ));
        $config->provider('gemini', array(
            'replay' => array(
                'enabled' => true,
            ),
        ));

        $resolver = new ConfigResolver($config);

        $this->assertTrue($resolver->resolveProviderSetting('gemini', 'replay.enabled', false));
    }

    public function testGlobalEnvOverrideWinsOverConfig()
    {
        $config = new Config();
        $config->runtime(array(
            'replay' => array(
                'enabled' => false,
            ),
        ));

        putenv('AI_REPLAY_ENABLED=true');

        $resolver = new ConfigResolver($config);

        $this->assertTrue($resolver->resolveProviderSetting('openai', 'replay.enabled', false));
    }

    public function testProviderEnvOverrideWinsOverGlobalEnvAndProviderConfig()
    {
        $config = new Config();
        $config->runtime(array(
            'replay' => array(
                'enabled' => false,
            ),
        ));
        $config->provider('gemini', array(
            'replay' => array(
                'enabled' => false,
            ),
        ));

        putenv('AI_REPLAY_ENABLED=false');
        putenv('GEMINI_REPLAY_ENABLED=true');

        $resolver = new ConfigResolver($config);

        $this->assertTrue($resolver->resolveProviderSetting('gemini', 'replay.enabled', false));
    }

    public function testCastsIntegerEnvOverrides()
    {
        $resolver = new ConfigResolver(new Config());
        putenv('AI_STREAM_READ_SIZE=256');
        putenv('GEMINI_STREAM_READ_SIZE=128');

        $this->assertSame(128, $resolver->resolveProviderSetting('gemini', 'stream_read_size', 1024));
    }
}
