import { sqliteTable, text, integer, index } from 'drizzle-orm/sqlite-core'
import { user } from '@/server/modules/auth/db/schema'
import { softDeleteColumn } from '@/server/lib/soft-delete'

export const comments = sqliteTable('comments', {
  id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
  entityType: text('entity_type').notNull(),
  entityId: text('entity_id').notNull(),
  userId: text('user_id').notNull().references(() => user.id, { onDelete: 'cascade' }),
  body: text('body').notNull(),
  parentId: text('parent_id'), // nullable — for threaded replies
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
  ...softDeleteColumn,
}, (table) => [
  index('comments_entity_idx').on(table.entityType, table.entityId),
  index('comments_user_id_idx').on(table.userId),
  index('comments_parent_id_idx').on(table.parentId),
])
