import type { Db } from '../client'

/**
 * Tenant-scoped query factory.
 * ALL route handlers operating on tenant data MUST use this factory.
 * Only this module (packages/db/src/queries/**) may import raw Drizzle tables.
 *
 * This is the primary multi-tenant isolation mechanism at v1.
 * Postgres RLS is deferred to post-v1; this wrapper + ESLint enforcement
 * is the structural defense.
 *
 * Returns domain helper objects pre-bound to tenantId.
 * Downstream specs add their helpers to the returned object.
 */
export interface TenantQueryResult {
  _db: Db
  _tenantId: string
}

export function tenantQuery(db: Db, tenantId: string): TenantQueryResult {
  // Domain helpers are added per spec, e.g.:
  // tasks: tasksQuery(db, tenantId),
  // invoices: invoicesQuery(db, tenantId),
  return {
    _db: db,
    _tenantId: tenantId,
    // Populated by downstream specs
  }
}
