/**
 * Income record generator.
 *
 * Produces IncomeRecord entries spread across 24 months, referencing
 * existing Work IDs with realistic territories, media types, and sources.
 */

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

const TERRITORIES = [
  'US', 'UK', 'DE', 'FR', 'JP', 'BR', 'AU', 'CA', 'MX', 'ES', 'IT', 'KR',
] as const;

const MEDIA_TYPES = [
  'Streaming', 'Download', 'Physical', 'Sync', 'Performance', 'Broadcast',
] as const;

const SOURCES: IncomeRecord['source'][] = ['PRO', 'DSP', 'sub-publisher'];

/**
 * Generate IncomeRecord entries.
 *
 * @param rng   - Seeded PRNG instance
 * @param count - Number of income records to generate (≥500 recommended)
 * @param works - Previously generated Work records to reference
 */
export function generateIncome(rng: SeededRng, count: number, works: Work[]): IncomeRecord[] {
  const records: IncomeRecord[] = [];
  const startDate = new Date('2024-01-01');
  const endDate = new Date('2025-12-31');

  for (let i = 0; i < count; i++) {
    const id = `INC-${String(i + 1).padStart(5, '0')}`;
    const grossAmount = Math.round(rng.randomFloat(10, 10000) * 100) / 100;
    const deductionRate = rng.randomFloat(0.05, 0.35);
    const netAmount = Math.round(grossAmount * (1 - deductionRate) * 100) / 100;

    records.push({
      id,
      workId: rng.pick(works).id,
      amount: netAmount,
      grossAmount,
      netAmount,
      territory: rng.pick(TERRITORIES),
      mediaType: rng.pick(MEDIA_TYPES),
      source: rng.pick(SOURCES),
      date: rng.randomDate(startDate, endDate),
    });
  }

  return records;
}
