import { describe, it, expect } from 'vitest';
import { parseCSVToAnnotation } from './importUtils';

describe('parseCSVToAnnotation', () => {
  it('parses semicolon-delimited CSV with BOM, header aliases, and checkbox state', async () => {
    const csv = '\uFEFF"Model (External ID)";"Annotator";"File Hash";"Save Date";"Atlas";"Checked By";"Trait 1"\n' +
      '"model-1";"User";"hash";"N/A";"Atlas CS";"Checker";"Value 1"';

    const file = new File([csv], 'import.csv', { type: 'text/csv' });

    const megaSchema = [{
      atlas_id: 'atlas1',
      znaky: [
        {
          id: 'trait-1',
          label_cs: 'Trait 1',
          label_en: 'Trait 1',
          options: [
            { label_cs: 'Value 1', label_en: 'Value 1' }
          ]
        }
      ]
    }];

    const allAtlases = [
      { id: 'atlas1', nazev_cs: 'Atlas CS', nazev_en: 'Atlas EN' }
    ];

    const result = await parseCSVToAnnotation(file, megaSchema, 'cs', allAtlases);

    expect(result).toEqual([
      {
        modelId: 'model-1',
        annotatorName: 'User',
        fileHash: 'hash',
        atlasId: 'atlas1',
        atlasName: 'Atlas CS',
        checkerName: 'Checker',
        isChecked: true,
        values: { 'trait-1': 'Value 1' }
      }
    ]);
  });

  it('rejects invalid CSV content that contains only a header row', async () => {
    const csv = '"Model (External ID)";"Annotator";"File Hash"\n';
    const file = new File([csv], 'invalid.csv', { type: 'text/csv' });

    await expect(parseCSVToAnnotation(file, [], 'cs', [])).rejects.toThrow('Soubor je prázdný.');
  });
});
