<?php

declare(strict_types=1);

namespace App\Administration\Application\Service\Import;

use App\Administration\Domain\Model\Import\KnownImportFormat;
use App\Administration\Domain\Model\Import\TeacherImportField;

use function in_array;

/**
 * Suggère un mapping automatique des colonnes pour l'import enseignants.
 *
 * @see AC2: Mapping spécifique enseignants
 */
final readonly class TeacherColumnMappingSuggester
{
    /**
     * Mapping générique par mots-clés pour les enseignants.
     *
     * @var array<string, TeacherImportField>
     */
    private const array GENERIC_KEYWORDS = [
        'nom' => TeacherImportField::LAST_NAME,
        'last' => TeacherImportField::LAST_NAME,
        'family' => TeacherImportField::LAST_NAME,
        'surname' => TeacherImportField::LAST_NAME,
        'prénom' => TeacherImportField::FIRST_NAME,
        'prenom' => TeacherImportField::FIRST_NAME,
        'first' => TeacherImportField::FIRST_NAME,
        'given' => TeacherImportField::FIRST_NAME,
        'email' => TeacherImportField::EMAIL,
        'mail' => TeacherImportField::EMAIL,
        'courriel' => TeacherImportField::EMAIL,
        'matière' => TeacherImportField::SUBJECTS,
        'matiere' => TeacherImportField::SUBJECTS,
        'matières' => TeacherImportField::SUBJECTS,
        'matieres' => TeacherImportField::SUBJECTS,
        'subject' => TeacherImportField::SUBJECTS,
        'discipline' => TeacherImportField::SUBJECTS,
        'classe' => TeacherImportField::CLASSES,
        'classes' => TeacherImportField::CLASSES,
        'class' => TeacherImportField::CLASSES,
        'groupe' => TeacherImportField::CLASSES,
    ];

    /**
     * @param list<string>      $columns        Colonnes détectées dans le fichier
     * @param KnownImportFormat $detectedFormat Format détecté
     *
     * @return array<string, TeacherImportField> Mapping suggéré (colonne → champ)
     */
    public function suggerer(array $columns, KnownImportFormat $detectedFormat): array
    {
        return $this->mapperGenerique($columns);
    }

    /**
     * @param list<string> $columns
     *
     * @return array<string, TeacherImportField>
     */
    private function mapperGenerique(array $columns): array
    {
        $mapping = [];
        $usedFields = [];

        foreach ($columns as $column) {
            $normalized = $this->normaliser($column);

            foreach (self::GENERIC_KEYWORDS as $keyword => $field) {
                if (str_contains($normalized, $keyword) && !in_array($field, $usedFields, true)) {
                    $mapping[$column] = $field;
                    $usedFields[] = $field;
                    break;
                }
            }
        }

        return $mapping;
    }

    private function normaliser(string $column): string
    {
        $normalized = mb_strtolower(trim($column));
        $normalized = str_replace(['_', '-', "'"], [' ', ' ', ' '], $normalized);

        /** @var string $result */
        $result = preg_replace('/\s+/', ' ', $normalized);

        return $result;
    }
}
