import { bigint, boolean, index, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core" import { tenants } from "./tenants" import { files } from "./files" import { sql } from "drizzle-orm" export const scannerInboxConnections = pgTable("scanner_inbox_connections", { tenantId: bigint("tenant_id", { mode: "number" }).primaryKey().references(() => tenants.id, { onDelete: "cascade" }), username: text("username").notNull().unique(), passwordHash: text("password_hash").notNull(), active: boolean("active").notNull().default(true), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }), }) export const scannerInboxDocuments = pgTable("scanner_inbox_documents", { fileId: uuid("file_id").primaryKey().references(() => files.id, { onDelete: "cascade" }), tenantId: bigint("tenant_id", { mode: "number" }).notNull().references(() => tenants.id, { onDelete: "cascade" }), remoteFilename: text("remote_filename").notNull(), checksum: text("checksum").notNull(), receivedAt: timestamp("received_at", { withTimezone: true }).notNull().defaultNow(), filedAt: timestamp("filed_at", { withTimezone: true }), suggestionStatus: text("suggestion_status").notNull().default("pending"), suggestion: jsonb("suggestion"), suggestionMessage: text("suggestion_message"), suggestionClaim: uuid("suggestion_claim"), suggestionUpdatedAt: timestamp("suggestion_updated_at", { withTimezone: true }).notNull().defaultNow(), }, table => ({ filenameKey: uniqueIndex("scanner_inbox_documents_filename_key").on(table.tenantId, table.remoteFilename), receivedIdx: index("scanner_inbox_documents_received_idx").on(table.tenantId, table.receivedAt), suggestionQueueIdx: index("scanner_inbox_suggestion_queue_idx").on(table.suggestionStatus, table.suggestionUpdatedAt, table.receivedAt).where(sql`${table.filedAt} IS NULL`), }))