/**
 * Pre-generates Bible verse data for all rosary mystery references.
 * Run with: npx vite-node scripts/generate-mystery-verses.ts
 */
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import { lookupReference } from '../src/lib/server/bible';
import { mysteryReferences, mysteryReferencesEnglish, theologicalVirtueReference, theologicalVirtueReferenceEnglish } from '../src/lib/data/mysteryDescriptions';
import type { MysteryDescription, VerseData } from '../src/lib/data/mysteryDescriptions';

function generateVerseData(
	references: Record<string, readonly { title: string; reference: string }[]>,
	tsvPath: string
): Record<string, MysteryDescription[]> {
	const result: Record<string, MysteryDescription[]> = {};

	for (const [mysteryType, refs] of Object.entries(references)) {
		const descriptions: MysteryDescription[] = [];

		for (const ref of refs) {
			const lookup = lookupReference(ref.reference, tsvPath);

			let text = '';
			let verseData: VerseData | null = null;

			if (lookup && lookup.verses.length > 0) {
				text = `«${lookup.verses.map((v) => v.text).join(' ')}»`;
				verseData = {
					book: lookup.book,
					chapter: lookup.chapter,
					verses: lookup.verses
				};
			} else {
				console.warn(`No verses found for: ${ref.reference} in ${tsvPath}`);
			}

			descriptions.push({
				title: ref.title,
				reference: ref.reference,
				text,
				verseData
			});
		}

		result[mysteryType] = descriptions;
	}

	return result;
}

const dePath = resolve('static/allioli.tsv');
const enPath = resolve('static/drb.tsv');

const mysteryVerseDataDe = generateVerseData(mysteryReferences, dePath);
const mysteryVerseDataEn = generateVerseData(mysteryReferencesEnglish, enPath);

// Generate theological virtue (1 Cor 13) verse data
function generateSingleRef(ref: { title: string; reference: string }, tsvPath: string): MysteryDescription {
	const lookup = lookupReference(ref.reference, tsvPath);
	let text = '';
	let verseData: VerseData | null = null;
	if (lookup && lookup.verses.length > 0) {
		text = `«${lookup.verses.map((v) => v.text).join(' ')}»`;
		verseData = { book: lookup.book, chapter: lookup.chapter, verses: lookup.verses };
	} else {
		console.warn(`No verses found for: ${ref.reference} in ${tsvPath}`);
	}
	return { title: ref.title, reference: ref.reference, text, verseData };
}

const theologicalVirtueDataDe = generateSingleRef(theologicalVirtueReference, dePath);
const theologicalVirtueDataEn = generateSingleRef(theologicalVirtueReferenceEnglish, enPath);

const output = `// Auto-generated by scripts/generate-mystery-verses.ts — do not edit manually
import type { MysteryDescription } from './mysteryDescriptions';

export const mysteryVerseDataDe: Record<string, MysteryDescription[]> = ${JSON.stringify(mysteryVerseDataDe, null, '\t')};

export const mysteryVerseDataEn: Record<string, MysteryDescription[]> = ${JSON.stringify(mysteryVerseDataEn, null, '\t')};

export const theologicalVirtueVerseDataDe: MysteryDescription = ${JSON.stringify(theologicalVirtueDataDe, null, '\t')};

export const theologicalVirtueVerseDataEn: MysteryDescription = ${JSON.stringify(theologicalVirtueDataEn, null, '\t')};
`;

const outPath = resolve('src/lib/data/mysteryVerseData.ts');
writeFileSync(outPath, output, 'utf-8');
console.log(`Wrote mystery verse data to ${outPath}`);
