/**
 * Audit event generator.
 *
 * Produces AuditEvent records with timestamps, entity types, action types,
 * and user names covering at least 12 months.
 */

import type { SeededRng } from '@/lib/mock-data/seed';
import type { AuditEvent } from '@/lib/mock-data/types';

const ENTITY_TYPES = [
  'Work', 'Composer', 'Payee', 'Contract', 'Income',
  'Transaction', 'Cost', 'Statement', 'SuspenseItem', 'User',
] as const;

const ACTION_TYPES: AuditEvent['actionType'][] = ['create', 'update', 'delete'];

const USER_NAMES = [
  'admin@divisi.com',
  'carlos.garcia@divisi.com',
  'maria.silva@divisi.com',
  'james.smith@divisi.com',
  'yuki.tanaka@divisi.com',
  'sofia.mueller@divisi.com',
  'system',
] as const;

/**
 * Generate AuditEvent records.
 *
 * @param rng   - Seeded PRNG instance
 * @param count - Number of audit events to generate (≥100 recommended)
 */
export function generateAuditEvents(rng: SeededRng, count: number): AuditEvent[] {
  const events: AuditEvent[] = [];
  // Cover at least 12 months — we use the full 24-month window
  const startDate = new Date('2024-01-01');
  const endDate = new Date('2025-12-31');

  for (let i = 0; i < count; i++) {
    const id = `AUD-${String(i + 1).padStart(5, '0')}`;

    // Weight towards 'update' actions (most common in real systems)
    const actionRoll = rng.next();
    let actionType: AuditEvent['actionType'];
    if (actionRoll < 0.2) {
      actionType = 'create';
    } else if (actionRoll < 0.85) {
      actionType = 'update';
    } else {
      actionType = 'delete';
    }

    events.push({
      id,
      timestamp: rng.randomDate(startDate, endDate),
      entityType: rng.pick(ENTITY_TYPES),
      actionType,
      userName: rng.pick(USER_NAMES),
    });
  }

  return events;
}
