import {
  init_ids,
  newId
} from "./chunk-F4WUVOLE.js";
import {
  init_clock,
  nowMs,
  toIso
} from "./chunk-HFDLUBWN.js";
import {
  getDb,
  init_client
} from "./chunk-TLEKKGH7.js";

// src/db/tenants.ts
init_client();
init_ids();
init_clock();
var DEFAULT_TENANT_ID = "tenant_default";
function hydrate(row) {
  return {
    id: row.id,
    name: row.name,
    created_at: toIso(row.created_at),
    archived_at: row.archived_at ? toIso(row.archived_at) : null
  };
}
function seedDefaultTenant() {
  const db = getDb();
  db.prepare(
    `INSERT OR IGNORE INTO tenants (id, name, created_at) VALUES (?, ?, ?)`
  ).run(DEFAULT_TENANT_ID, "default", nowMs());
}
function createTenant(input) {
  const db = getDb();
  const id = input.id ?? newId("tenant");
  const now = nowMs();
  db.prepare(
    `INSERT INTO tenants (id, name, created_at) VALUES (?, ?, ?)`
  ).run(id, input.name, now);
  return { id, name: input.name, created_at: toIso(now), archived_at: null };
}
function getTenant(id) {
  const db = getDb();
  const row = db.prepare(`SELECT * FROM tenants WHERE id = ?`).get(id);
  return row ? hydrate(row) : null;
}
function listTenants(opts = {}) {
  const db = getDb();
  const rows = opts.includeArchived ? db.prepare(`SELECT * FROM tenants ORDER BY created_at ASC`).all() : db.prepare(`SELECT * FROM tenants WHERE archived_at IS NULL ORDER BY created_at ASC`).all();
  return rows.map(hydrate);
}
function archiveTenant(id) {
  if (id === DEFAULT_TENANT_ID) return false;
  const db = getDb();
  const res = db.prepare(`UPDATE tenants SET archived_at = ? WHERE id = ? AND archived_at IS NULL`).run(nowMs(), id);
  return res.changes > 0;
}
function renameTenant(id, name) {
  const db = getDb();
  const res = db.prepare(`UPDATE tenants SET name = ? WHERE id = ?`).run(name, id);
  return res.changes > 0;
}
function assignNullRowsToTenant(tenantId) {
  const db = getDb();
  const counts = { agents: 0, environments: 0, vaults: 0, sessions: 0, proxy_resources: 0 };
  const tx = db.transaction(() => {
    for (const table of ["agents", "environments", "vaults", "sessions", "proxy_resources"]) {
      const res = db.prepare(`UPDATE ${table} SET tenant_id = ? WHERE tenant_id IS NULL`).run(tenantId);
      counts[table] = res.changes;
    }
  });
  tx();
  return counts;
}
function countNullTenantRows() {
  const db = getDb();
  const c = (sql) => db.prepare(sql).get().n;
  return {
    agents: c(`SELECT COUNT(*) AS n FROM agents           WHERE tenant_id IS NULL AND archived_at IS NULL`),
    environments: c(`SELECT COUNT(*) AS n FROM environments     WHERE tenant_id IS NULL AND archived_at IS NULL`),
    vaults: c(`SELECT COUNT(*) AS n FROM vaults           WHERE tenant_id IS NULL`),
    sessions: c(`SELECT COUNT(*) AS n FROM sessions         WHERE tenant_id IS NULL`),
    api_keys: c(`SELECT COUNT(*) AS n FROM api_keys         WHERE tenant_id IS NULL AND revoked_at IS NULL`),
    proxy_resources: c(`SELECT COUNT(*) AS n FROM proxy_resources  WHERE tenant_id IS NULL`)
  };
}

export {
  DEFAULT_TENANT_ID,
  seedDefaultTenant,
  createTenant,
  getTenant,
  listTenants,
  archiveTenant,
  renameTenant,
  assignNullRowsToTenant,
  countNullTenantRows
};
