import { sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core'
import { sql } from 'drizzle-orm'

export const channelBindings = sqliteTable(
  'channel_bindings',
  {
    id: text('id').primaryKey(),
    channelId: text('channel_id').notNull(),
    chatId: text('chat_id').notNull(),
    chatName: text('chat_name').notNull().default(''),
    workspace: text('workspace').notNull().default(''),
    workspaceId: text('workspace_id').notNull().default(''),
    sessionId: text('session_id'),
    createdAt: text('created_at').notNull().default(sql`(datetime('now'))`),
    updatedAt: text('updated_at').notNull().default(sql`(datetime('now'))`),
  },
  (t) => ({
    channelChatUnique: uniqueIndex('idx_channel_bindings_channel_chat').on(t.channelId, t.chatId),
  }),
)

export type ChannelBinding = typeof channelBindings.$inferSelect
export type NewChannelBinding = typeof channelBindings.$inferInsert
