const { validateCsvConfig } = require('../src/lib/csvConfigValidation');

const okBuckets = [
  { name: 'Success', type: 'note', notes: ['s'] },
  { name: 'Failure', type: 'note', notes: ['f'] },
  { name: 'Other',   type: 'note', notes: [] },
];

describe('validateCsvConfig', () => {
  it('accepts a valid config with the three default buckets', () => {
    expect(validateCsvConfig({ buckets: okBuckets })).toBeNull();
  });

  it('accepts a valid config with extra note + numerical buckets', () => {
    const buckets = [
      ...okBuckets,
      { name: 'Lick latency', type: 'numerical', notes: ['L25'] },
      { name: 'Bonus',        type: 'note',      notes: ['b'] },
    ];
    expect(validateCsvConfig({ buckets })).toBeNull();
  });

  it('rejects when body is not an object', () => {
    expect(validateCsvConfig(null)).toMatch(/object/i);
    expect(validateCsvConfig('x')).toMatch(/object/i);
  });

  it('rejects when buckets is not an array', () => {
    expect(validateCsvConfig({ buckets: 'nope' })).toMatch(/array/i);
  });

  it('rejects an empty bucket name', () => {
    const buckets = [...okBuckets, { name: '   ', type: 'note', notes: [] }];
    expect(validateCsvConfig({ buckets })).toMatch(/name/i);
  });

  it('rejects duplicate bucket names', () => {
    const buckets = [...okBuckets, { name: 'Success', type: 'note', notes: [] }];
    expect(validateCsvConfig({ buckets })).toMatch(/duplicate/i);
  });

  it('rejects an invalid type value', () => {
    const buckets = [...okBuckets, { name: 'X', type: 'weird', notes: [] }];
    expect(validateCsvConfig({ buckets })).toMatch(/type/i);
  });

  it('rejects when notes is not an array', () => {
    const buckets = [...okBuckets, { name: 'X', type: 'note', notes: 'oops' }];
    expect(validateCsvConfig({ buckets })).toMatch(/notes/i);
  });

  it('rejects when notes contains non-strings', () => {
    const buckets = [...okBuckets, { name: 'X', type: 'note', notes: [1, 2] }];
    expect(validateCsvConfig({ buckets })).toMatch(/notes/i);
  });

  it('rejects when a default bucket is missing', () => {
    const buckets = okBuckets.filter((b) => b.name !== 'Success');
    expect(validateCsvConfig({ buckets })).toMatch(/Success/);
  });
});
