import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { listAudioCache, listAudioJobs, type AudioCacheEntry, type AudioJob } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';

export const load: PageServerLoad = async ({ locals }) => {
	if (locals.user?.role !== 'admin') {
		redirect(302, '/');
	}

	const [entries, jobs] = await Promise.all([
		listAudioCache().catch((e): AudioCacheEntry[] => {
			log.warn('admin/audio', 'failed to load audio cache', { err: String(e) });
			return [];
		}),
		listAudioJobs().catch((e): AudioJob[] => {
			log.warn('admin/audio', 'failed to load audio jobs', { err: String(e) });
			return [];
		})
	]);

	return { entries, jobs };
};
