import { browser } from '$app/environment';
import type { PageLoad } from './$types';

// TODO: change to env var
const API_HOST = `${browser ? '' : 'http://proxy-admin-local'}/api/v1`;


interface Job {
	id: Number;
	targetSiteName: string;
	createdTs: string;
	completedTs: string;
	auctionsFound: Number;
	errors: string;
}

interface ScrapeStatusPageData {
	jobs: Job[]
}

export const load = (async ({ fetch, url }): Promise<ScrapeStatusPageData> => {
	try {
		const response = await fetch(API_HOST + `/sync`);
		const {results } = await response.json();
		return {
			jobs: results    || []
		};
	} catch (e) {
		console.log(e);
		return { jobs:[] };
	}
}) satisfies PageLoad;
