<?php

declare(strict_types=1);

namespace App\Tests\Unit\Mail;

use App\Entity\MailConfiguration;
use App\Mail\Exception\MailProviderException;
use App\Mail\ImapMailProvider;
use App\Repository\MailConfigurationRepository;
use App\Service\TokenEncryptor;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;

/**
 * @internal
 */
class ImapMailProviderTest extends TestCase
{
    public function testThrowsWhenConfigDisabled(): void
    {
        $config = new MailConfiguration();
        $config->setEnabled(false);

        $repo = $this->createMock(MailConfigurationRepository::class);
        $repo->method('findSingleton')->willReturn($config);

        $provider = new ImapMailProvider($repo, $this->makeEncryptor(), new NullLogger());

        $this->expectException(MailProviderException::class);
        $provider->listFolders();
    }

    public function testThrowsWhenConfigMissing(): void
    {
        $repo = $this->createMock(MailConfigurationRepository::class);
        $repo->method('findSingleton')->willReturn(null);

        $provider = new ImapMailProvider($repo, $this->makeEncryptor(), new NullLogger());

        $this->expectException(MailProviderException::class);
        $provider->listFolders();
    }

    private function makeEncryptor(): TokenEncryptor
    {
        return new TokenEncryptor(sodium_bin2hex(random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES)));
    }
}
