/**
 * Onboarding read helpers — tenant-scoped queries used by onboarding routes.
 * Services may import @zync/db directly (no-raw-drizzle-from-routes applies to routes only).
 */
import { count, eq } from '@zync/db'
import type { Db } from '@zync/db'
import { invitations } from '@zync/db/schema'
import { loadAdapterCredentialRow } from '@zync/db/queries'

export async function deriveEmailAdapter(
  db: Db,
  tenantId: string,
): Promise<'smtp' | null> {
  const row = await loadAdapterCredentialRow(db, tenantId, 'smtp')
  return row ? 'smtp' : null
}

export async function countTenantInvitations(db: Db, tenantId: string): Promise<number> {
  const [row] = await db
    .select({ cnt: count(invitations.id) })
    .from(invitations)
    .where(eq(invitations.tenantId, tenantId))
  return Number(row?.cnt ?? 0)
}
