<?php

use App\Services\CsvImportService;

it('detects semicolon-delimited headers after leading blanks', function () {
    $tmpPath = storage_path('app/test_csv_detection_semicolon.csv');
    $content = "\n\n\nCol A;Col B;Col C\n1;2;3\n";
    // Prepend UTF-8 BOM to simulate common exports
    $content = "\xEF\xBB\xBF".$content;
    file_put_contents($tmpPath, $content);

    $svc = new CsvImportService;
    [$delim, $cols] = $svc->detectColumnsFromCsv($tmpPath, true);

    expect($delim)->toBe(';');
    expect($cols)->toBe(['Col A', 'Col B', 'Col C']);

    @unlink($tmpPath);
});

it('returns positional indices when hasHeader is false', function () {
    $tmpPath = storage_path('app/test_csv_detection_positional.csv');
    $content = "\n\nA;B;C\n";
    file_put_contents($tmpPath, $content);

    $svc = new CsvImportService;
    [$delim, $cols] = $svc->detectColumnsFromCsv($tmpPath, false);

    expect($delim)->toBe(';');
    expect($cols)->toBe(['0', '1', '2']);

    @unlink($tmpPath);
});
