import { test, expect } from "bun:test";
import {
  createDb,
  setDb,
  getDb,
  hasDb,
  registerDb,
  databaseNames,
  closeDatabases,
} from "../src/index.ts";
import { sqlite } from "../src/adapters/sqlite.ts";

test("multi-database registry: default + named connections", async () => {
  await closeDatabases(); // isolate from any prior state

  const main = createDb(sqlite(":memory:"));
  const analytics = createDb(sqlite(":memory:"));

  setDb(main); // default
  registerDb("analytics", analytics); // named

  expect(getDb()).toBe(main);
  expect(getDb("analytics")).toBe(analytics);
  expect(hasDb()).toBe(true);
  expect(hasDb("analytics")).toBe(true);
  expect(hasDb("missing")).toBe(false);
  expect(databaseNames().sort()).toEqual(["analytics", "default"]);

  // Each connection is independent — a table in one is not in the other.
  await main.exec("CREATE TABLE a (id INTEGER)");
  await analytics.exec("CREATE TABLE b (id INTEGER)");
  await getDb().exec("INSERT INTO a (id) VALUES (1)");
  await getDb("analytics").exec("INSERT INTO b (id) VALUES (2)");
  expect((await getDb().all("SELECT id FROM a")).length).toBe(1);
  expect((await getDb("analytics").all("SELECT id FROM b")).length).toBe(1);

  await closeDatabases();
  expect(hasDb()).toBe(false);
  expect(databaseNames()).toEqual([]);
});

test("getDb throws a helpful error for an unknown named database", async () => {
  await closeDatabases();
  setDb(createDb(sqlite(":memory:")));
  expect(() => getDb("nope")).toThrow(/No database named 'nope'/);
  await closeDatabases();
});
