<?php

declare(strict_types=1);

namespace App\Tests\Unit\Mail;

use App\Module\Mail\Domain\Entity\MailConfiguration;
use App\Module\Mail\Domain\Exception\MailProviderException;
use App\Module\Mail\Domain\Repository\MailConfigurationRepositoryInterface;
use App\Module\Mail\Infrastructure\Imap\ImapMailProvider;
use App\Shared\Infrastructure\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(MailConfigurationRepositoryInterface::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(MailConfigurationRepositoryInterface::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)));
    }
}
