/**
 * FORWARD DECLARATION of the `audit_log` table (spec 28, audit-compliance).
 *
 * audit-compliance OWNS this table: it is created by a hand-written, partitioned,
 * trigger-protected SQL migration and exposes the canonical `export const auditLog`
 * pgTable from `@zync/db`. That spec is NOT yet built in this wave.
 *
 * foundation-auth-rbac must write an audit row IN THE SAME TRANSACTION as every
 * tenant-scoped write (acceptance criterion; enforced by `require-audit-in-transaction`).
 * To do that with full type-safety BEFORE spec 28 lands, we forward-declare the
 * exact column shape spec 28 pins (docs/plans/tasks/audit-compliance.md, Task 2).
 *
 * CRITICAL MERGE-SAFETY CONTRACT:
 *  - This file lives OUTSIDE `src/schema/` so drizzle-kit (schema glob: ./src/schema)
 *    does NOT emit a duplicate `CREATE TABLE audit_log` into our migration. The
 *    partitioned table is created exclusively by spec 28's hand-written SQL.
 *  - `auditLog` is NOT re-exported from the `@zync/db` barrel, so there is no
 *    public-surface symbol collision when spec 28 adds the canonical export.
 *  - When audit-compliance lands, delete this file and import `auditLog` from the
 *    schema barrel instead. The column shape here matches 1:1, so call sites are stable.
 */
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'), // 'user'|'system'|'api_key'|'portal_customer'
  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]> | null>(),
  requestId: text('request_id'),
  ip: text('ip'),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export type NewAuditLog = typeof auditLog.$inferInsert
