import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';

export const load: PageServerLoad = async ({ fetch, params }) => {
	const id = params.id;
	const res = await fetch(`/api/v1/libs/${encodeURIComponent(id)}`);

	if (res.status === 404) {
		error(404, 'Repository not found');
	}

	if (!res.ok) {
		error(res.status, 'Failed to load repository');
	}

	const repo = await res.json();

	// Fetch recent jobs
	const jobsRes = await fetch(`/api/v1/jobs?repositoryId=${encodeURIComponent(id)}&limit=5`);
	const jobsData = jobsRes.ok ? await jobsRes.json() : { jobs: [] };

	return {
		repo,
		recentJobs: jobsData.jobs ?? []
	};
};
