/**
 * Payee record generator.
 *
 * Produces Payee records with types, territories, and cumulative earnings.
 */

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

const PAYEE_TYPES = ['publisher', 'sub-publisher', 'administrator', 'individual'] as const;

const TERRITORIES = [
  'US', 'UK', 'DE', 'FR', 'JP', 'BR', 'AU', 'CA', 'MX', 'ES', 'IT', 'KR',
  'NL', 'SE', 'NO', 'DK', 'CH', 'AT', 'PT', 'AR',
] as const;

const PUBLISHER_NAMES = [
  'Universal Music Publishing', 'Sony/ATV Music', 'Warner Chappell',
  'BMG Rights Management', 'Kobalt Music', 'Concord Music',
  'Downtown Music', 'Peermusic', 'Spirit Music Group', 'Reservoir Media',
] as const;

const COMPANY_SUFFIXES = [
  'Publishing', 'Music Group', 'Rights Management', 'Entertainment',
  'Media', 'Music Corp', 'Editions', 'Musikverlag', 'Ediciones',
] as const;

const INDIVIDUAL_FIRST = [
  'Alex', 'Jordan', 'Taylor', 'Morgan', 'Casey',
  'Riley', 'Quinn', 'Avery', 'Reese', 'Dakota',
] as const;

const INDIVIDUAL_LAST = [
  'Rivera', 'Chen', 'Okafor', 'Petrov', 'Larsson',
  'Tanaka', 'Morales', 'Fischer', 'Dubois', 'Santos',
] as const;

function generatePayeeName(rng: SeededRng, type: Payee['type']): string {
  if (type === 'individual') {
    return `${rng.pick(INDIVIDUAL_FIRST)} ${rng.pick(INDIVIDUAL_LAST)}`;
  }
  if (rng.next() < 0.4) {
    return rng.pick(PUBLISHER_NAMES);
  }
  const territory = rng.pick(TERRITORIES);
  return `${territory} ${rng.pick(COMPANY_SUFFIXES)}`;
}

/**
 * Generate Payee records.
 *
 * @param rng   - Seeded PRNG instance
 * @param count - Number of payees to generate (≥40 recommended)
 */
export function generatePayees(rng: SeededRng, count: number): Payee[] {
  const payees: Payee[] = [];

  for (let i = 0; i < count; i++) {
    const id = `PAY-${String(i + 1).padStart(4, '0')}`;
    const type = rng.pick(PAYEE_TYPES);

    payees.push({
      id,
      name: generatePayeeName(rng, type),
      type,
      territory: rng.pick(TERRITORIES),
      cumulativeEarnings: Math.round(rng.randomFloat(1000, 500000) * 100) / 100,
    });
  }

  return payees;
}
