const { test } = require('node:test');
const assert = require('node:assert/strict');

const transfer = require('../src/utils/extensionTransfer');

test('extension export payload removes ids and keeps portable fields', () => {
  const payload = transfer.exportPayload([
    { id: 10, location: ' ED ', name: ' Charge ', number: ' 1234 ', type: 'pager', notes: ' Nights ' },
    { id: 11, location: '', name: 'Invalid', number: '5555', type: 'extension' }
  ]);

  assert.equal(payload.format, 'pedscribe-phone-directory');
  assert.equal(payload.version, 1);
  assert.deepEqual(payload.items, [
    { location: 'ED', name: 'Charge', number: '1234', type: 'pager', notes: 'Nights' }
  ]);
});

test('extension import normalizes, clips, and deduplicates entries', () => {
  const items = transfer.importItemsFromBody({
    items: [
      { location: 'ED', name: 'Charge', number: '1234', type: 'pager', notes: 'Nights' },
      { location: ' ed ', name: ' charge ', number: ' 1234 ', type: 'pager', notes: ' nights ' },
      { location: 'Ward', name: 'Desk', number: '9999', type: 'unknown', notes: null },
      { location: '', name: 'Bad', number: '1111' }
    ]
  });

  assert.deepEqual(items, [
    { location: 'ED', name: 'Charge', number: '1234', type: 'pager', notes: 'Nights' },
    { location: 'Ward', name: 'Desk', number: '9999', type: 'extension', notes: '' }
  ]);
});

test('extension transfer zip round-trips JSON payload', () => {
  const payload = transfer.exportPayload([
    { location: 'NICU', name: 'Fellow', number: '7777', type: 'pager', notes: '' }
  ]);
  const zip = transfer.createJsonZip('pedscribe-extensions.json', payload);
  const parsed = transfer.parseImportBuffer(zip);

  assert.deepEqual(parsed.items, payload.items);
});

test('extension import analysis flags exact and loose duplicates', () => {
  const items = transfer.importItemsFromBody({
    items: [
      { location: 'ED', name: 'Charge', number: '1234', type: 'pager', notes: 'Nights' },
      { location: 'ED', name: 'Desk', number: '1234', type: 'extension', notes: '' },
      { location: 'Ward', name: 'Clerk', number: '1234', type: 'pager', notes: '' },
      { location: 'NICU', name: 'Fellow', number: '7777', type: 'pager', notes: '' }
    ]
  });
  const analysis = transfer.analyzeImport(items, [
    { id: 1, location: ' ed ', name: ' charge ', number: '1234', type: 'pager', notes: 'nights', trashed_at: null },
    { id: 2, location: 'NICU', name: 'Fellow', number: '7777', type: 'pager', notes: '', trashed_at: '2026-05-08T00:00:00Z' }
  ]);

  assert.equal(analysis.summary.exactActive, 1);
  assert.equal(analysis.summary.exactTrashed, 1);
  assert.equal(analysis.summary.possible, 2);
  assert.deepEqual(analysis.entries.map(e => e.status), [
    'exact_active',
    'possible_duplicate',
    'possible_duplicate',
    'exact_trashed'
  ]);
});

test('extension import analysis flags loose duplicates within the same import file', () => {
  const items = transfer.importItemsFromBody({
    items: [
      { location: 'ED', name: 'Desk', number: '1234', type: 'extension', notes: '' },
      { location: 'ED', name: 'Nurse', number: '1234', type: 'extension', notes: 'alternate label' }
    ]
  });
  const analysis = transfer.analyzeImport(items, []);

  assert.equal(analysis.summary.new, 1);
  assert.equal(analysis.summary.possible, 1);
  assert.deepEqual(analysis.entries.map(e => e.status), ['new', 'possible_duplicate']);
});
