import 'fake-indexeddb/auto';
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { IndexedDBStorage } from '../src/index.js';

function uniqueDbName(): string {
  return `shade-test-${Date.now()}-${Math.random().toString(36).slice(2)}`;
}

async function deleteDb(dbName: string): Promise<void> {
  await new Promise<void>((resolve, reject) => {
    const req = indexedDB.deleteDatabase(dbName);
    req.onsuccess = () => resolve();
    req.onerror = () => reject(req.error);
    req.onblocked = () => resolve();
  });
}

describe('IndexedDBStorage — peer_verifications (V3.3)', () => {
  let dbName: string;
  let storage: IndexedDBStorage;

  beforeEach(async () => {
    dbName = uniqueDbName();
    storage = await IndexedDBStorage.create({ dbName });
  });

  afterEach(async () => {
    await storage.close();
    await deleteDb(dbName);
  });

  test('round trip: save → get → remove', async () => {
    await storage.savePeerVerification({
      peerAddress: 'bob',
      fingerprint: '12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 67890',
      verifiedAt: 1_700_000_000_000,
      verifiedBy: 'user',
      identityVersion: 1,
    });

    const v = await storage.getPeerVerification('bob');
    expect(v).not.toBeNull();
    expect(v!.peerAddress).toBe('bob');
    expect(v!.verifiedBy).toBe('user');
    expect(v!.identityVersion).toBe(1);

    await storage.removePeerVerification('bob');
    expect(await storage.getPeerVerification('bob')).toBeNull();
  });

  test('upsert overwrites on duplicate peer_address', async () => {
    await storage.savePeerVerification({
      peerAddress: 'bob',
      fingerprint: 'fp-1',
      verifiedAt: 1,
      verifiedBy: 'user',
      identityVersion: 1,
    });
    await storage.savePeerVerification({
      peerAddress: 'bob',
      fingerprint: 'fp-2',
      verifiedAt: 2,
      verifiedBy: 'transitive',
      identityVersion: 2,
    });

    const v = await storage.getPeerVerification('bob');
    expect(v!.fingerprint).toBe('fp-2');
    expect(v!.verifiedBy).toBe('transitive');
    expect(v!.identityVersion).toBe(2);
  });

  test('identity-version starts at 1 and increments via bump', async () => {
    expect(await storage.getPeerIdentityVersion('alice')).toBe(1);
    expect(await storage.bumpPeerIdentityVersion('alice')).toBe(2);
    expect(await storage.bumpPeerIdentityVersion('alice')).toBe(3);
    expect(await storage.getPeerIdentityVersion('alice')).toBe(3);
    // Independent counter per peer
    expect(await storage.getPeerIdentityVersion('bob')).toBe(1);
  });

  test('survives reopen', async () => {
    await storage.savePeerVerification({
      peerAddress: 'bob',
      fingerprint: 'fp',
      verifiedAt: 42,
      verifiedBy: 'user',
      identityVersion: 5,
    });
    await storage.bumpPeerIdentityVersion('bob');
    await storage.close();

    storage = await IndexedDBStorage.create({ dbName });
    const v = await storage.getPeerVerification('bob');
    expect(v!.fingerprint).toBe('fp');
    expect(v!.identityVersion).toBe(5);
    expect(await storage.getPeerIdentityVersion('bob')).toBe(2);
  });
});
