import assert from 'node:assert/strict';
import test from 'node:test';

import {
  parseLanguageTable,
  parsePlayerModels,
  validateRetailUiCatalog,
} from '../server/retail-ui-catalog.js';

const manifest = { revision: 'q4retail-test', fingerprint: 'a'.repeat(64) };

test('retail player declarations become a localized, team-aware model catalog', () => {
  const language = parseLanguageTable(`{
    "#str_108003" "Kane"
    "#str_108014" "Stroggified Kane"
  }`);
  const models = parsePlayerModels(`
    playerModel model_player_marine {
      "team" "marine"
      "description" "#str_108003"
    }
    playerModel model_player_kane_strogg {
      "team" "strogg"
      "description" "#str_108014"
    }
    playerModel model_player_failed_transfer {
      "description" "#str_missing"
    }
  `, language);

  assert.deepEqual(models, [
    { id: 'model_player_failed_transfer', label: 'Failed Transfer', team: null },
    { id: 'model_player_marine', label: 'Kane', team: 'Marine' },
    { id: 'model_player_kane_strogg', label: 'Stroggified Kane', team: 'Strogg' },
  ]);
});

test('retail UI catalog binds to its containing manifest and rejects duplicate or injectable model ids', () => {
  const valid = {
    schema: 1,
    ...manifest,
    models: [{ id: 'model_player_marine', label: 'Kane', team: 'Marine' }],
  };
  assert.deepEqual(validateRetailUiCatalog(valid, manifest).models, valid.models);
  assert.throws(() => validateRetailUiCatalog(valid, null), /does not match/);
  assert.throws(() => validateRetailUiCatalog({
    ...valid,
    models: [{ id: '+quit', label: 'Bad', team: null }],
  }, manifest), /invalid player model/);
  assert.throws(() => validateRetailUiCatalog({
    ...valid,
    models: [...valid.models, valid.models[0]],
  }, manifest), /invalid player model/);
});
