<?php

namespace MintyPHP\Tests\Service\Settings;

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

class SettingsMetadataGatewayTest extends TestCase
{
    public function testGetValueDelegatesToRepository(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->expects($this->once())
            ->method('getValue')
            ->with('some_key')
            ->willReturn('some_value');

        $gateway = new SettingsMetadataGateway($settings);
        $this->assertSame('some_value', $gateway->getValue('some_key'));
    }

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

        $gateway = new SettingsMetadataGateway($settings);
        $this->assertSame(42, $gateway->getInt('numeric_key'));
    }

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

        $gateway = new SettingsMetadataGateway($settings);
        $this->assertNull($gateway->getInt('empty_key'));
    }

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

        $gateway = new SettingsMetadataGateway($settings);
        $this->assertNull($gateway->getInt('missing_key'));
    }

    public function testSetDelegatesToRepository(): void
    {
        $settings = $this->createMock(SettingRepositoryInterface::class);
        $settings->expects($this->once())
            ->method('set')
            ->with('my_key', 'my_value', 'description text')
            ->willReturn(true);

        $gateway = new SettingsMetadataGateway($settings);
        $this->assertTrue($gateway->set('my_key', 'my_value', 'description text'));
    }
}
