import { pgTable, text, timestamp, pgEnum, index } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import { users } from "./users";
import { recipes } from "./recipes";

export const chatRoleEnum = pgEnum("chat_role", ["user", "assistant"]);

// The general cooking assistant (recipeId null on chatMessages) can have
// multiple named conversations per user — the per-recipe chat doesn't need
// this, since each recipe is naturally its own single thread already.
export const aiConversations = pgTable("ai_conversations", {
  id: text("id").primaryKey(),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  title: text("title"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at").notNull().defaultNow(),
}, (t) => [
  index("ai_conversations_user_idx").on(t.userId, t.updatedAt),
]);

export const aiConversationsRelations = relations(aiConversations, ({ one, many }) => ({
  user: one(users, { fields: [aiConversations.userId], references: [users.id] }),
  messages: many(chatMessages),
}));

// Persists both the per-recipe chat (recipeId set) and the general cooking
// assistant on the recipes homepage (recipeId null) — same table, since a
// user searching "their chat history" expects both to show up together.
// conversationId is only set for general-assistant messages created after
// multi-conversation support shipped; older general messages and all
// per-recipe messages leave it null.
export const chatMessages = pgTable("chat_messages", {
  id: text("id").primaryKey(),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  recipeId: text("recipe_id").references(() => recipes.id, { onDelete: "cascade" }),
  conversationId: text("conversation_id").references(() => aiConversations.id, { onDelete: "cascade" }),
  role: chatRoleEnum("role").notNull(),
  content: text("content").notNull(),
  createdAt: timestamp("created_at").notNull().defaultNow(),
}, (t) => [
  index("chat_messages_user_idx").on(t.userId, t.createdAt),
  index("chat_messages_recipe_idx").on(t.recipeId),
  index("chat_messages_conversation_idx").on(t.conversationId),
]);

export const chatMessagesRelations = relations(chatMessages, ({ one }) => ({
  user: one(users, { fields: [chatMessages.userId], references: [users.id] }),
  recipe: one(recipes, { fields: [chatMessages.recipeId], references: [recipes.id] }),
  conversation: one(aiConversations, { fields: [chatMessages.conversationId], references: [aiConversations.id] }),
}));
