<?php

namespace MintyPHP\Tests\Module\Security\Service;

use MintyPHP\Module\Security\Repository\SecurityTokenRepository;
use MintyPHP\Module\Security\Service\SecurityBcSettingsGateway;
use MintyPHP\Module\Security\Service\SecurityOAuthTokenService;
use PHPUnit\Framework\TestCase;

class SecurityOAuthTokenServiceTest extends TestCase
{
    public function testReturnsNullWhenAuthModeIsBasic(): void
    {
        $settings = $this->createMock(SecurityBcSettingsGateway::class);
        $settings->method('getAuthMode')->willReturn(SecurityBcSettingsGateway::AUTH_MODE_BASIC);
        $repo = $this->createMock(SecurityTokenRepository::class);
        $repo->expects($this->never())->method('findValidToken');

        $service = new SecurityOAuthTokenService($settings, $repo);

        $this->assertNull($service->getAccessToken(1));
    }

    public function testReturnsNullForInvalidTenant(): void
    {
        $settings = $this->createMock(SecurityBcSettingsGateway::class);
        $settings->method('getAuthMode')->willReturn(SecurityBcSettingsGateway::AUTH_MODE_OAUTH2);
        $repo = $this->createMock(SecurityTokenRepository::class);

        $service = new SecurityOAuthTokenService($settings, $repo);

        $this->assertNull($service->getAccessToken(0));
    }

    public function testReturnsCachedTokenWhenAvailable(): void
    {
        $settings = $this->createMock(SecurityBcSettingsGateway::class);
        $settings->method('getAuthMode')->willReturn(SecurityBcSettingsGateway::AUTH_MODE_OAUTH2);
        $repo = $this->createMock(SecurityTokenRepository::class);
        $repo->method('findValidToken')->willReturn('cached-bearer-token');

        $service = new SecurityOAuthTokenService($settings, $repo);

        $this->assertSame('cached-bearer-token', $service->getAccessToken(1));
    }
}
