import assert from 'node:assert/strict';
import test from 'node:test';
import { assertFileExists, assertHeadersMatchTemplate, countRows, parseCsv, readJson } from './helpers.ts';

interface DatasetManifest {
  files: string[];
  counts: Record<string, number>;
  expectedQuotationStatuses: string[];
}

test('golden dataset files exist and match template headers', () => {
  const manifest = readJson<DatasetManifest>('setup/golden-dataset/dataset.json');

  assertFileExists('setup/golden-dataset/README.md');
  for (const fileName of manifest.files) {
    assertFileExists(`setup/golden-dataset/${fileName}`);
    assertHeadersMatchTemplate(fileName);
  }
});

test('golden dataset satisfies required record counts', () => {
  const manifest = readJson<DatasetManifest>('setup/golden-dataset/dataset.json');

  assert.equal(countRows('setup/golden-dataset/organizations.csv'), manifest.counts.organizations);
  assert.equal(countRows('setup/golden-dataset/users.csv'), manifest.counts.users);
  assert.equal(countRows('setup/golden-dataset/customers.csv'), manifest.counts.customers);
  assert.equal(countRows('setup/golden-dataset/contacts.csv'), manifest.counts.contacts);
  assert.equal(countRows('setup/golden-dataset/leads.csv'), manifest.counts.leads);
  assert.equal(countRows('setup/golden-dataset/opportunities.csv'), manifest.counts.opportunities);
  assert.equal(countRows('setup/golden-dataset/quotations.csv'), manifest.counts.quotations);
  assert.equal(countRows('setup/golden-dataset/branches.csv'), manifest.counts.branches);
  assert.equal(countRows('setup/golden-dataset/product-types.csv'), manifest.counts.productTypes);
});

test('golden quotation statuses cover setup approval and revision scenarios', () => {
  const manifest = readJson<DatasetManifest>('setup/golden-dataset/dataset.json');
  const quotations = parseCsv('setup/golden-dataset/quotations.csv');
  const statusIndex = quotations.header.indexOf('status');
  const statuses = new Set(quotations.rows.map((row) => row[statusIndex]));

  for (const status of manifest.expectedQuotationStatuses) {
    assert.equal(statuses.has(status), true, `quotation status ${status} should be present`);
  }
});
