import { readFileSync, existsSync } from 'fs';
import { resolve } from 'path';
import { applyCachedEmbyNames } from '../lib/server/emby-user-cache.js';

/** @type {import('./$types').PageServerLoad} */
export async function load() {
	const dataPath = resolve('static/db_export.json');
	const raw = readFileSync(dataPath, 'utf-8');
	const data = JSON.parse(raw);
	const enrichedUsers = applyCachedEmbyNames(data.users);

	const configPath = resolve('config.json');
	let config = { embyUrl: '', apiKey: '', tmdbApiKey: '', dbPath: '' };
	if (existsSync(configPath)) {
		try {
			config = {
				...config,
				...JSON.parse(readFileSync(configPath, 'utf-8'))
			};
		} catch { /* use defaults */ }
	}

	return {
		users: enrichedUsers.users,
		genres: data.genres,
		enums: data.enums,
		config,
		embyCache: enrichedUsers.cache
	};
}
