import { pgTable, text, timestamp, uuid, bigint } from "drizzle-orm/pg-core"

import { authUsers } from "./auth_users"
import { tenants } from "./tenants"

export const authRefreshTokens = pgTable("auth_refresh_tokens", {
    id: uuid("id").primaryKey().defaultRandom(),
    userId: uuid("user_id")
        .notNull()
        .references(() => authUsers.id, { onDelete: "cascade", onUpdate: "cascade" }),
    tenantId: bigint("tenant_id", { mode: "number" })
        .references(() => tenants.id, { onDelete: "set null", onUpdate: "cascade" }),
    tokenHash: text("token_hash").notNull().unique(),
    createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
    expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
    revokedAt: timestamp("revoked_at", { withTimezone: true }),
})

export type AuthRefreshToken = typeof authRefreshTokens.$inferSelect
export type NewAuthRefreshToken = typeof authRefreshTokens.$inferInsert
