import type { H3Event } from 'h3' import { useDatabase } from './db' export function writeAuditLog(event: H3Event, input: { operatorId?: number action: string entityType: string entityKey?: string metadata?: Record }) { const db = useDatabase() db.prepare(` INSERT INTO audit_logs (operator_id, action, entity_type, entity_key, ip_address, metadata_json, created_at) VALUES (?, ?, ?, ?, ?, ?, ?) `).run( input.operatorId ?? null, input.action, input.entityType, input.entityKey ?? null, getRequestIP(event, { xForwardedFor: true }) ?? null, JSON.stringify(input.metadata ?? {}), new Date().toISOString(), ) }