<?php

declare(strict_types=1);

use App\Services\Supplier\SupplierCsvParser;

function rowsOf(iterable $gen): array
{
    $out = [];
    foreach ($gen as $row) {
        $out[] = $row;
    }

    return $out;
}

it('parses 3-column Name;Tag;Phone CSV', function (): void {
    $csv = "Name;Tag;Phone\nB1_a.com;tagA;79991234567\nB2_79990001122;tagB;79993334455\n";
    $rows = rowsOf((new SupplierCsvParser)->parse($csv));

    expect($rows)->toHaveCount(2);
    expect($rows[0])->toBe(['project' => 'B1_a.com', 'tag' => 'tagA', 'phone' => '79991234567']);
    expect($rows[1])->toBe(['project' => 'B2_79990001122', 'tag' => 'tagB', 'phone' => '79993334455']);
});

it('strips UTF-8 BOM and normalizes CRLF', function (): void {
    $csv = "\xEF\xBB\xBFName;Tag;Phone\r\nB1_a.com;t;79991234567\r\n";
    $rows = rowsOf((new SupplierCsvParser)->parse($csv));

    expect($rows)->toHaveCount(1);
    expect($rows[0]['project'])->toBe('B1_a.com');
});

it('skips malformed rows with fewer than 3 columns', function (): void {
    $csv = "Name;Tag;Phone\nB1_a.com;t;79991234567\nbroken;row\nB2_b.com;t2;79990000000\n";
    $rows = rowsOf((new SupplierCsvParser)->parse($csv));

    expect($rows)->toHaveCount(2);
    expect($rows[1]['project'])->toBe('B2_b.com');
});

it('returns nothing for empty CSV', function (): void {
    expect(rowsOf((new SupplierCsvParser)->parse('')))->toBe([]);
});

it('returns nothing for header-only CSV', function (): void {
    expect(rowsOf((new SupplierCsvParser)->parse("Name;Tag;Phone\n")))->toBe([]);
});
