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

import { createUpdateDeleteTimestamps, orderKeyColumns, orderKeyIndex, uuidPrimaryKey } from './_columnHelpers'
import { userModelTable } from './userModel'

export const agentTable = sqliteTable(
  'agent',
  {
    id: uuidPrimaryKey(),
    type: text().notNull(),
    name: text().notNull(),
    description: text().notNull().default(''),
    instructions: text().notNull(),
    model: text().references(() => userModelTable.id, { onDelete: 'set null' }),
    planModel: text().references(() => userModelTable.id, { onDelete: 'set null' }),
    smallModel: text().references(() => userModelTable.id, { onDelete: 'set null' }),
    disabledTools: text({ mode: 'json' }).$type<string[]>().notNull().default(sql`'[]'`),
    configuration: text({ mode: 'json' }).$type<Record<string, unknown>>().notNull().default(sql`'{}'`),
    ...orderKeyColumns,
    ...createUpdateDeleteTimestamps
  },
  (t) => [index('agent_name_idx').on(t.name), index('agent_type_idx').on(t.type), orderKeyIndex('agent')(t)]
)

export type AgentRow = typeof agentTable.$inferSelect
export type InsertAgentRow = typeof agentTable.$inferInsert
