<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Domain\Model\Import;

use App\Administration\Domain\Model\Import\ImportRow;
use App\Administration\Domain\Model\Import\ImportRowError;
use App\Administration\Domain\Model\Import\StudentImportField;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class ImportRowTest extends TestCase
{
    #[Test]
    public function rowSansErreurEstValide(): void
    {
        $row = new ImportRow(
            lineNumber: 1,
            rawData: ['Nom' => 'Dupont', 'Prénom' => 'Jean'],
            mappedData: ['lastName' => 'Dupont', 'firstName' => 'Jean'],
        );

        self::assertTrue($row->estValide());
        self::assertSame(1, $row->lineNumber);
    }

    #[Test]
    public function rowAvecErreursEstInvalide(): void
    {
        $row = new ImportRow(
            lineNumber: 3,
            rawData: ['Nom' => '', 'Prénom' => 'Jean'],
            mappedData: ['lastName' => '', 'firstName' => 'Jean'],
            errors: [new ImportRowError('lastName', 'Le nom est obligatoire')],
        );

        self::assertFalse($row->estValide());
        self::assertCount(1, $row->errors);
    }

    #[Test]
    public function valeurChampReturnsMappedValue(): void
    {
        $row = new ImportRow(
            lineNumber: 1,
            rawData: [],
            mappedData: ['lastName' => 'Dupont', 'firstName' => 'Jean'],
        );

        self::assertSame('Dupont', $row->valeurChamp(StudentImportField::LAST_NAME));
        self::assertSame('Jean', $row->valeurChamp(StudentImportField::FIRST_NAME));
        self::assertNull($row->valeurChamp(StudentImportField::EMAIL));
    }

    #[Test]
    public function avecErreursCreatesNewRowWithAdditionalErrors(): void
    {
        $row = new ImportRow(
            lineNumber: 1,
            rawData: [],
            mappedData: ['lastName' => ''],
            errors: [new ImportRowError('lastName', 'Le nom est obligatoire')],
        );

        $newRow = $row->avecErreurs(
            new ImportRowError('email', 'Email invalide'),
        );

        self::assertCount(1, $row->errors);
        self::assertCount(2, $newRow->errors);
        self::assertSame($row->lineNumber, $newRow->lineNumber);
    }
}
