<?php

declare(strict_types=1);

namespace App\Tests\Unit\Mail;

use App\Module\Mail\Infrastructure\Imap\MimeHeaderDecoder;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 */
class MimeHeaderDecoderTest extends TestCase
{
    public function testDecodesQEncodedSubject(): void
    {
        self::assertSame(
            'Fwd: Votre inscription',
            MimeHeaderDecoder::decode('=?UTF-8?Q?Fwd=3A_Votre_inscription?='),
        );
    }

    public function testDecodesBEncodedSubjectWithAccents(): void
    {
        // "Réunion été" encodé en Base64 UTF-8
        self::assertSame(
            'Réunion été',
            MimeHeaderDecoder::decode('=?UTF-8?B?UsOpdW5pb24gw6l0w6k=?='),
        );
    }

    public function testIsIdempotentOnPlainText(): void
    {
        self::assertSame('Christian ROY', MimeHeaderDecoder::decode('Christian ROY'));
        self::assertSame('TR: Bail commercial', MimeHeaderDecoder::decode('TR: Bail commercial'));
    }

    public function testPreservesNullAndEmpty(): void
    {
        self::assertNull(MimeHeaderDecoder::decode(null));
        self::assertSame('', MimeHeaderDecoder::decode(''));
    }

    public function testFallsBackToOriginalWhenUndecodable(): void
    {
        // Charset inconnu : on ne perd pas la valeur d'origine.
        $value = '=?unknown-charset?Q?test?=';
        self::assertNotSame('', MimeHeaderDecoder::decode($value));
    }
}
