import { pgTable, uuid, bigint, timestamp } from "drizzle-orm/pg-core"
import { authUsers } from "./auth_users"
import { authRoles } from "./auth_roles"

export const authUserRoles = pgTable(
    "auth_user_roles",
    {
        created_at: timestamp("created_at", { withTimezone: true })
            .notNull()
            .defaultNow(),

        user_id: uuid("user_id")
            .notNull()
            .references(() => authUsers.id),

        role_id: uuid("role_id")
            .notNull()
            .references(() => authRoles.id),

        tenant_id: bigint("tenant_id", { mode: "number" }).notNull(),

        created_by: uuid("created_by").references(() => authUsers.id),
    },
    (table) => ({
        primaryKey: [table.user_id, table.role_id, table.tenant_id],
    })
)

export type AuthUserRole = typeof authUserRoles.$inferSelect
export type NewAuthUserRole = typeof authUserRoles.$inferInsert
