<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Application\Service\Import;

use App\Administration\Application\Service\Import\TeacherColumnMappingSuggester;
use App\Administration\Domain\Model\Import\KnownImportFormat;
use App\Administration\Domain\Model\Import\TeacherImportField;

use function count;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

use const SORT_REGULAR;

final class TeacherColumnMappingSuggesterTest extends TestCase
{
    private TeacherColumnMappingSuggester $suggester;

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

    #[Test]
    public function suggestGenericMappingByKeywords(): void
    {
        $columns = ['Nom', 'Prénom', 'Email', 'Matières', 'Classes'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        self::assertSame(TeacherImportField::LAST_NAME, $mapping['Nom']);
        self::assertSame(TeacherImportField::FIRST_NAME, $mapping['Prénom']);
        self::assertSame(TeacherImportField::EMAIL, $mapping['Email']);
        self::assertSame(TeacherImportField::SUBJECTS, $mapping['Matières']);
        self::assertSame(TeacherImportField::CLASSES, $mapping['Classes']);
    }

    #[Test]
    public function suggestHandlesEnglishColumnNames(): void
    {
        $columns = ['Last Name', 'First Name', 'Email', 'Subject', 'Class'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        self::assertSame(TeacherImportField::LAST_NAME, $mapping['Last Name']);
        self::assertSame(TeacherImportField::FIRST_NAME, $mapping['First Name']);
        self::assertSame(TeacherImportField::EMAIL, $mapping['Email']);
        self::assertSame(TeacherImportField::SUBJECTS, $mapping['Subject']);
        self::assertSame(TeacherImportField::CLASSES, $mapping['Class']);
    }

    #[Test]
    public function suggestNormalizesAccentsAndCase(): void
    {
        $columns = ['NOM', 'PRÉNOM', 'EMAIL', 'MATIÈRES', 'CLASSES'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        self::assertSame(TeacherImportField::LAST_NAME, $mapping['NOM']);
        self::assertSame(TeacherImportField::FIRST_NAME, $mapping['PRÉNOM']);
        self::assertSame(TeacherImportField::EMAIL, $mapping['EMAIL']);
        self::assertSame(TeacherImportField::SUBJECTS, $mapping['MATIÈRES']);
        self::assertSame(TeacherImportField::CLASSES, $mapping['CLASSES']);
    }

    #[Test]
    public function suggestDoesNotDuplicateFields(): void
    {
        $columns = ['Nom', 'Nom de famille', 'Prénom', 'Email'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        $mappedFields = array_values($mapping);
        $uniqueFields = array_unique($mappedFields, SORT_REGULAR);

        self::assertCount(count($uniqueFields), $mappedFields);
    }

    #[Test]
    public function suggestHandlesUnknownColumns(): void
    {
        $columns = ['ColonneInconnue', 'AutreColonne', 'Nom', 'Email'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        self::assertArrayNotHasKey('ColonneInconnue', $mapping);
        self::assertArrayNotHasKey('AutreColonne', $mapping);
        self::assertArrayHasKey('Nom', $mapping);
        self::assertArrayHasKey('Email', $mapping);
    }

    #[Test]
    public function suggestHandsDisciplineKeyword(): void
    {
        $columns = ['Nom', 'Prénom', 'Courriel', 'Discipline'];

        $mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);

        self::assertSame(TeacherImportField::EMAIL, $mapping['Courriel']);
        self::assertSame(TeacherImportField::SUBJECTS, $mapping['Discipline']);
    }
}
