/* test/platformContactsRepo.test.js
 *
 * Validates the learned-contact-map:
 *   - getBest only returns rows with confidence >= MIN_CONFIDENCE_TO_USE
 *   - recordObservedSuccess promotes confidence to 1.0
 *   - recordFailure downgrades confidence
 *   - recordAdminOverride sets confidence=1.0
 *   - the curated KNOWN map is REDUCED to the test fixture only
 *     (the historical hand-written pornhub/onlyfans/etc. guesses are
 *     not in KNOWN any more — this test pins that invariant)
 */
'use strict';

const assert = require('assert');
const { setupFreshDb, db } = require('./_setup');

const TESTS = [];
const test = (n, fn) => TESTS.push({ name: n, fn });

const repo  = require('../services/platformContactsRepo');
const contacts = require('../services/platformContacts');

test('curated KNOWN map is reduced to the test fixture only', () => {
  setupFreshDb();
  const keys = Object.keys(contacts.KNOWN || {});
  // Only 127.0.0.1 is allowed in the curated map, and only when
  // test mode is on. The 15 historical hand-written entries must
  // be gone.
  assert.strictEqual(keys.length, 0, `curated KNOWN map should be empty, got: ${keys.join(',')}`);
});

test('getBest returns null for an unknown domain with no DB row', () => {
  setupFreshDb();
  const r = repo.getBest('totally-unknown-domain-xyz.example');
  assert.strictEqual(r, null, 'must not return a guessed address for an unknown domain');
});

test('recordAdminOverride is the only path that produces a directly-usable contact (confidence=1.0)', () => {
  setupFreshDb();
  const c = repo.recordAdminOverride({ domain: 'platform.test', contact: 'abuse@platform.test' });
  assert.ok(c);
  assert.strictEqual(c.source, 'admin_override');
  assert.strictEqual(c.confidence, 1.0);
  // And getBest returns it
  const best = repo.getBest('platform.test');
  assert.ok(best);
  assert.strictEqual(best.contact, 'abuse@platform.test');
});

test('recordDiscovery from RDAP with confidence=0.5 is selectable, 0.3 is not', () => {
  setupFreshDb();
  repo.recordDiscovery({ domain: 'whois1.test', contact: 'a@whois1.test', source: 'whois', confidence: 0.5 });
  assert.ok(repo.getBest('whois1.test'), '0.5 confidence should be selectable');
  repo.recordDiscovery({ domain: 'whois2.test', contact: 'a@whois2.test', source: 'whois', confidence: 0.3 });
  assert.strictEqual(repo.getBest('whois2.test'), null, '0.3 confidence should NOT be selectable (below floor)');
});

test('recordObservedSuccess promotes the address to confidence=1.0', () => {
  setupFreshDb();
  repo.recordAdminOverride({ domain: 'obs.test', contact: 'good@obs.test' });
  const c = repo.recordObservedSuccess({ domain: 'obs.test', contact: 'good@obs.test' });
  assert.strictEqual(c.source, 'observed_success');
  assert.strictEqual(c.confidence, 1.0);
  assert.ok(c.verified_at > 0);
});

test('recordFailure downgrades confidence (does not zero it)', () => {
  setupFreshDb();
  repo.recordAdminOverride({ domain: 'fail.test', contact: 'x@fail.test' });
  const before = repo.getBest('fail.test').confidence;
  repo.recordFailure({ domain: 'fail.test', contact: 'x@fail.test', reason: 'bounce' });
  const after = repo.getBest('fail.test').confidence;
  assert.ok(after < before, `confidence should drop: ${before} -> ${after}`);
  assert.ok(after >= 0.1, 'confidence should not drop below 0.1');
});

test('adminDisable sets confidence to 0 so the contact is no longer selectable', () => {
  setupFreshDb();
  const c = repo.recordAdminOverride({ domain: 'dis.test', contact: 'd@dis.test' });
  repo.adminDisable(c.id);
  assert.strictEqual(repo.getBest('dis.test'), null);
});

test('list filters by domain / source / minConfidence', () => {
  setupFreshDb();
  repo.recordAdminOverride({ domain: 'a.test', contact: 'a@a.test' });
  repo.recordAdminOverride({ domain: 'b.test', contact: 'b@b.test' });
  repo.recordDiscovery({ domain: 'c.test', contact: 'c@c.test', source: 'whois', confidence: 0.3 });
  assert.strictEqual(repo.list({ domain: 'a.test' }).length, 1);
  assert.strictEqual(repo.list({ source: 'admin_override' }).length, 2);
  assert.strictEqual(repo.list({ minConfidence: 0.5 }).length, 2);
});

test('the historical hand-written entries (pornhub, onlyfans, etc.) are NOT in the curated map', () => {
  setupFreshDb();
  const KNOWN = contacts.KNOWN || {};
  // Specific check — these are the entries the user called out as
  // "pulled from your ass". They must never be in the curated map
  // again. If you want to add them back, do it explicitly via
  // /admin/contacts with admin_override source.
  for (const d of ['pornhub.com','onlyfans.com','xhamster.com','chaturbate.com','fansly.com','brazzers.com','redtube.com','tube8.com','youporn.com','mofos.com','realitykings.com','bangbros.com','spankbang.com','motherless.com','xvideos.com']) {
    assert.strictEqual(KNOWN[d], undefined, `${d} must NOT be in curated KNOWN`);
  }
});

module.exports = TESTS;
