model AiGeneratedMessage { id String @id @default(cuid()) vehicleId String type String // "summary" | "common_issues" | etc. content String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt vehicle Vehicle @relation(fields: [vehicleId], references: [id], onDelete: Cascade) @@unique([vehicleId, type]) @@map("ai_generated_messages") } /// A conversation about one vehicle or one customer. The chat is scoped to /// that record on purpose: the model is handed the record's own data and /// nothing else, so it can only ever talk about what the page it was opened /// from already shows. Exactly one of vehicleId and customerId is set. model AiChat { id String @id @default(cuid()) title String @default("New chat") createdAt DateTime @default(now()) updatedAt DateTime @updatedAt userId String organizationId String vehicleId String? vehicle Vehicle? @relation(fields: [vehicleId], references: [id], onDelete: Cascade) customerId String? customer Customer? @relation(fields: [customerId], references: [id], onDelete: Cascade) messages AiChatMessage[] @@index([organizationId, userId, updatedAt(sort: Desc)]) @@index([vehicleId]) @@index([customerId]) @@map("ai_chats") } model AiChatMessage { id String @id @default(cuid()) role String // "user" | "assistant" content String createdAt DateTime @default(now()) chatId String chat AiChat @relation(fields: [chatId], references: [id], onDelete: Cascade) @@index([chatId, createdAt]) @@map("ai_chat_messages") }