<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\User\Import;

use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\User\Import\ClientImportAnalysis;
use CpCreation\VitiCore\User\Import\ClientImporter;
use CpCreation\VitiCore\User\Import\ClientImportRow;
use CpCreation\VitiCore\User\Model\ImportedClientGroup;
use CpCreation\VitiCore\User\Repository\ImportedClientGroupRepository;
use PHPUnit\Framework\TestCase;

final class ClientImporterTest extends TestCase
{
    public function testItCreatesAndUpdatesEmailGroupMappingsWithoutCreatingUsers(): void
    {
        $groupA = new ProductPriceGroup();
        $groupA->setName('Particuliers');
        $groupB = new ProductPriceGroup();
        $groupB->setName('Club');
        $existingItem = new ImportedClientGroup('existing@example.com', $groupB);
        $existingItem->setApplyOnRegistration(false);
        $unchangedItem = new ImportedClientGroup('unchanged@example.com', $groupB);

        $repository = $this->createMock(ImportedClientGroupRepository::class);
        $repository
            ->expects(self::once())
            ->method('findIndexedByEmails')
            ->willReturn([
                'existing@example.com' => $existingItem,
                'unchanged@example.com' => $unchangedItem,
            ]);

        $createdItems = [];
        $repository
            ->expects(self::once())
            ->method('persist')
            ->willReturnCallback(static function (ImportedClientGroup $item) use (&$createdItems): void {
                $createdItems[] = $item;
            });
        $repository->expects(self::once())->method('flush');

        $analysis = new ClientImportAnalysis(
            1,
            'A',
            'Email',
            'B',
            'Groupe',
            [
                new ClientImportRow(2, 'new@example.com', 'Source A'),
                new ClientImportRow(3, 'existing@example.com', 'Source A'),
                new ClientImportRow(4, 'new@example.com', 'Source B'),
                new ClientImportRow(5, 'unchanged@example.com', 'Source B'),
                new ClientImportRow(6, 'ignore@example.com', 'Source ignorée'),
            ],
            ['Source A', 'Source B', 'Source ignorée'],
            5,
            0,
            0,
        );

        $result = new ClientImporter($repository)->import($analysis, [
            'Source A' => $groupA,
            'Source B' => $groupB,
            'Source ignorée' => null,
        ]);

        self::assertSame(1, $result->created);
        self::assertSame(1, $result->updated);
        self::assertSame(1, $result->unchanged);
        self::assertSame(1, $result->ignored);
        self::assertSame(1, $result->duplicatesInFile);
        self::assertSame('new@example.com', $createdItems[0]->getEmail());
        self::assertSame($groupA, $createdItems[0]->getGroup());
        self::assertSame($groupA, $existingItem->getGroup());
        self::assertFalse($existingItem->shouldApplyOnRegistration());
        self::assertSame($groupB, $unchangedItem->getGroup());
    }
}
