import type { SQLWrapper } from 'drizzle-orm'

/** The host's drizzle schema. db core defines none — it is generic over this. */
export type Schema = Record<string, unknown>

export type Dialect = 'postgres' | 'sqlite'

/**
 * Dialect-agnostic query interface.
 * Both PgDatabase and BaseSQLiteDatabase (D1) satisfy this structurally.
 *
 * NOTE: execute returns Promise<unknown> because each driver has its own result type.
 * Use the concrete driver type if you need typed execute results.
 */
export interface QueryMethods<S extends Schema = Record<string, never>> {
  /** Schema type marker for type inference — not accessed at runtime */
  readonly _schema?: S
  select(): unknown
  insert(table: unknown): unknown
  update(table: unknown): unknown
  delete(table: unknown): unknown
  execute(query: SQLWrapper): Promise<unknown>
}

/** Query-capable handle WITHOUT interactive transactions. */
export type Database<S extends Schema = Record<string, never>> = QueryMethods<S>

/** Handle inside a .transaction() callback. */
export type Transaction<S extends Schema = Record<string, never>> = QueryMethods<S>

/** Adds interactive transactions. */
export type TransactionalDatabase<S extends Schema = Record<string, never>> = Database<S> & {
  transaction<T>(fn: (tx: Transaction<S>) => Promise<T>): Promise<T>
}

/** ANY query-capable handle — in or out of tx. Every query helper accepts this. */
export type Querier<S extends Schema = Record<string, never>> = Database<S> | Transaction<S>