/**
 * Suspense item generator.
 *
 * Produces SuspenseItem records with dates, source files, match types,
 * resolution statuses, and suggested Work ID references.
 */

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

const SOURCE_FILES = [
  'spotify_q1_2024.csv',
  'apple_music_q2_2024.csv',
  'youtube_music_2024.csv',
  'deezer_h1_2024.csv',
  'amazon_music_q3_2024.csv',
  'tidal_2024_annual.csv',
  'pandora_q4_2024.csv',
  'soundcloud_2025.csv',
  'spotify_q1_2025.csv',
  'apple_music_q2_2025.csv',
  'youtube_music_2025.csv',
  'deezer_h2_2025.csv',
] as const;

const MATCH_TYPES: SuspenseItem['matchType'][] = ['unmatched', 'ambiguous'];

const RESOLUTION_STATUSES: SuspenseItem['resolutionStatus'][] = ['pending', 'resolved'];

/**
 * Generate SuspenseItem records.
 *
 * @param rng   - Seeded PRNG instance
 * @param count - Number of suspense items to generate (≥40 recommended)
 * @param works - Previously generated Work records for suggestedWorkId
 */
export function generateSuspense(rng: SeededRng, count: number, works: Work[]): SuspenseItem[] {
  const items: SuspenseItem[] = [];
  const startDate = new Date('2024-01-01');
  const endDate = new Date('2025-12-31');

  for (let i = 0; i < count; i++) {
    const id = `SUS-${String(i + 1).padStart(4, '0')}`;
    const matchType = rng.pick(MATCH_TYPES);
    const resolutionStatus = rng.pick(RESOLUTION_STATUSES);

    // Ambiguous items are more likely to have a suggested work
    const hasSuggestion = matchType === 'ambiguous'
      ? rng.next() < 0.8
      : rng.next() < 0.3;

    items.push({
      id,
      date: rng.randomDate(startDate, endDate),
      sourceFile: rng.pick(SOURCE_FILES),
      matchType,
      resolutionStatus,
      suggestedWorkId: hasSuggestion ? rng.pick(works).id : null,
    });
  }

  return items;
}
