<?php

use App\Models\ImportEntity;
use App\Services\ImportProcessor;

it('groups multiple items per root when supports_multiple is true', function (): void {
    // Arrange: ensure ImportEntity supports_multiple for email
    ImportEntity::updateOrCreate(['key' => 'emails'], [
        'key' => 'emails',
        'canonical_root' => 'email',
        'label' => 'Emails',
        'fields' => ['value'],
        'aliases' => ['email', 'emails'],
        'supports_multiple' => true,
    ]);

    // Fake mappings: two email.value fields with groups 1 and 2
    $mappings = collect([
        (object) ['source_column' => 'Email 1', 'target_field' => 'email.value', 'transform' => null, 'apply_mode' => 'both', 'options' => ['group' => '1']],
        (object) ['source_column' => 'Email 2', 'target_field' => 'email.value', 'transform' => null, 'apply_mode' => 'both', 'options' => ['group' => '2']],
    ]);

    $proc = new class extends ImportProcessor
    {
        public function pubLoad(): array
        {
            return $this->loadImportEntityConfig();
        }

        public function pubApply(array $raw, $mappings, array $supports): array
        {
            return $this->applyMappings($raw, $mappings, $supports);
        }
    };

    [, , , $supports] = $proc->pubLoad();

    $raw = ['Email 1' => 'a@example.test', 'Email 2' => 'b@example.test'];
    [, $mapped] = $proc->pubApply($raw, $mappings, $supports);

    expect($mapped)
        ->toHaveKey('email')
        ->and($mapped['email'])
        ->toBeArray()
        ->and($mapped['email']['1']['value'] ?? null)->toBe('a@example.test')
        ->and($mapped['email']['2']['value'] ?? null)->toBe('b@example.test');
});

it('deduplicates grouped items within a single row', function (): void {
    ImportEntity::updateOrCreate(['key' => 'emails'], [
        'key' => 'emails', 'canonical_root' => 'email', 'label' => 'Emails', 'fields' => ['value'], 'aliases' => ['email', 'emails'], 'supports_multiple' => true,
    ]);
    $mappings = collect([
        (object) ['source_column' => 'Email 1', 'target_field' => 'email.value', 'transform' => null, 'apply_mode' => 'both', 'options' => ['group' => '1']],
        (object) ['source_column' => 'Email 2', 'target_field' => 'email.value', 'transform' => null, 'apply_mode' => 'both', 'options' => ['group' => '2']],
    ]);
    $proc = new class extends ImportProcessor
    {
        public function pubLoad(): array
        {
            return $this->loadImportEntityConfig();
        }

        public function pubApply(array $raw, $mappings, array $supports): array
        {
            return $this->applyMappings($raw, $mappings, $supports);
        }

        public function pubDedupe(array $grouped): array
        {
            return $this->dedupeGroupedItems($grouped, 'value', fn ($v) => is_null($v) ? null : mb_strtolower(trim((string) $v)));
        }
    };
    [, , , $supports] = $proc->pubLoad();
    $raw = ['Email 1' => 'a@example.test', 'Email 2' => 'A@EXAMPLE.TEST'];
    [, $mapped] = $proc->pubApply($raw, $mappings, $supports);
    $deduped = $proc->pubDedupe($mapped['email']);
    expect($deduped)->toHaveCount(1);
});
