import { pgTable, uuid, timestamp, index } from "drizzle-orm/pg-core";
import { chatFiles } from "./chat_files.js";
import { chatConversations } from "./chat_conversations.js";
import { chatMessages } from "./chat_messages.js";

export const chatFileReferences = pgTable(
  "chat_file_references",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    fileId: uuid("file_id").notNull().references(() => chatFiles.id, { onDelete: "cascade" }),
    conversationId: uuid("conversation_id").notNull().references(() => chatConversations.id, { onDelete: "cascade" }),
    messageId: uuid("message_id").references(() => chatMessages.id, { onDelete: "set null" }),
    createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  },
  (table) => ({
    fileIdx: index("chat_file_refs_file_idx").on(table.fileId),
    conversationIdx: index("chat_file_refs_conversation_idx").on(table.conversationId),
    messageIdx: index("chat_file_refs_message_idx").on(table.messageId),
  }),
);
