<?php

declare(strict_types=1);

namespace App\Administration\Domain\Model\Import;

use App\Administration\Domain\Exception\MappingIncompletException;

use function in_array;

/**
 * Value Object représentant l'association entre les colonnes du fichier source
 * et les champs Classeo pour l'import enseignants.
 *
 * @see FR77: Import enseignants via CSV
 */
final readonly class TeacherColumnMapping
{
    /**
     * @param array<string, TeacherImportField> $mapping Colonne source → champ Classeo
     */
    private function __construct(
        public array $mapping,
        public KnownImportFormat $format,
    ) {
    }

    /**
     * @param array<string, TeacherImportField> $mapping Colonne source → champ Classeo
     */
    public static function creer(array $mapping, KnownImportFormat $format): self
    {
        $mappedFields = array_values($mapping);
        $champsObligatoires = TeacherImportField::champsObligatoires();

        foreach ($champsObligatoires as $champ) {
            if (!in_array($champ, $mappedFields, true)) {
                throw MappingIncompletException::champManquant($champ);
            }
        }

        return new self($mapping, $format);
    }

    public function champPour(string $colonneSource): ?TeacherImportField
    {
        return $this->mapping[$colonneSource] ?? null;
    }

    /**
     * @return list<string>
     */
    public function colonnesSources(): array
    {
        return array_keys($this->mapping);
    }

    public function equals(self $other): bool
    {
        return $this->mapping === $other->mapping && $this->format === $other->format;
    }
}
