<?php

declare(strict_types=1);

namespace App\Tests\Module\Transport\Application;

use App\Module\Transport\Application\Service\CarrierFieldNormalizer;
use PHPUnit\Framework\TestCase;

/**
 * Normalisation serveur des champs texte du repertoire transporteurs (RG-4.13 +
 * cas LIOT RG-4.01). Jumeau de SupplierFieldNormalizerTest (M2), enrichi de
 * normalizeLiotPlates.
 *
 * @internal
 */
final class CarrierFieldNormalizerTest extends TestCase
{
    private CarrierFieldNormalizer $normalizer;

    protected function setUp(): void
    {
        $this->normalizer = new CarrierFieldNormalizer();
    }

    public function testNameIsUpperCasedAndTrimmed(): void
    {
        self::assertSame('TRANSPORTS X', $this->normalizer->normalizeName('  transports x  '));
        self::assertNull($this->normalizer->normalizeName(null));
    }

    public function testPersonNameIsTitleCased(): void
    {
        self::assertSame('Jean Dupont', $this->normalizer->normalizePersonName('JEAN dupont'));
        self::assertNull($this->normalizer->normalizePersonName('   '));
        self::assertNull($this->normalizer->normalizePersonName(null));
    }

    public function testEmailIsLowerCased(): void
    {
        self::assertSame('marie.martin@seed.test', $this->normalizer->normalizeEmail('  Marie.MARTIN@Seed.Test '));
        self::assertNull($this->normalizer->normalizeEmail('  '));
        self::assertNull($this->normalizer->normalizeEmail(null));
    }

    public function testPhoneKeepsDigitsOnly(): void
    {
        self::assertSame('0612345678', $this->normalizer->normalizePhone('06.12.34.56.78'));
        self::assertSame('33612345678', $this->normalizer->normalizePhone('+33 6 12 34 56 78'));
        self::assertNull($this->normalizer->normalizePhone('sans chiffre'));
        self::assertNull($this->normalizer->normalizePhone(null));
    }

    /**
     * RG-4.01 / RG-4.13 : la saisie « ; »-separee est decoupee, chaque plaque trim
     * + UPPER, segments vides ecartes, recomposee avec "; ".
     */
    public function testLiotPlatesAreSplitTrimmedUpperedAndRejoined(): void
    {
        self::assertSame(
            'AB-123-CD; EF-456-GH',
            $this->normalizer->normalizeLiotPlates('ab-123-cd ; ef-456-gh'),
        );
        // Segments vides (« ;; » / fin de chaine) ecartes.
        self::assertSame('AB-123-CD', $this->normalizer->normalizeLiotPlates(' ab-123-cd ; ; '));
        self::assertNull($this->normalizer->normalizeLiotPlates('  ;  ;  '));
        self::assertNull($this->normalizer->normalizeLiotPlates(null));
    }
}
