/**
 * Admin audit write helper — admin-dashboard.
 * Provides writeAdminAuditLog() for admin route handlers.
 * Uses the forward-declared auditLog table (_audit-forward.ts).
 */
import type { Db } from '../client'
import { auditLog } from './_audit-forward'

export interface AdminAuditInput {
  tenantId: string
  actorId: string
  entityType: string
  entityId: string
  action: string
  changes?: Record<string, [unknown, unknown]>
  ip?: string | null
}

/**
 * Write a row to audit_log from an admin action.
 * actor_type is always 'admin' for this helper.
 */
export async function writeAdminAuditLog(db: Db, input: AdminAuditInput): Promise<void> {
  await db.insert(auditLog).values({
    tenantId: input.tenantId,
    actorId: input.actorId,
    actorType: 'admin',
    entityType: input.entityType,
    entityId: input.entityId,
    action: input.action,
    changes: input.changes ?? null,
    ip: input.ip ?? null,
  })
}
