/**
 * audit_log Drizzle object — audit-compliance (wave-11 leaf-D).
 *
 * The partitioned table DDL, triggers, and indexes are in raw SQL migration
 * 0015_audit_log.sql. This object exists ONLY for type-safe inserts.
 * drizzle-kit must NOT generate migrations for this table.
 */
import { pgTable, uuid, text, jsonb, timestamp } from 'drizzle-orm/pg-core'

export const auditLog = pgTable('audit_log', {
  id:         uuid('id').notNull().defaultRandom(),
  tenantId:   uuid('tenant_id').notNull(),
  actorId:    uuid('actor_id'),
  actorType:  text('actor_type').notNull().default('user'),
  apiKeyId:   uuid('api_key_id'),
  entityType: text('entity_type').notNull(),
  entityId:   uuid('entity_id').notNull(),
  action:     text('action').notNull(),
  changes:    jsonb('changes').$type<Record<string, [unknown, unknown]>>(),
  requestId:  text('request_id'),
  ip:         text('ip'),
  createdAt:  timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export type AuditLogRow = typeof auditLog.$inferSelect
export type NewAuditLog = typeof auditLog.$inferInsert
