// #region ValidationTaskNewPageLoad [C:2] [TYPE Function] [SEMANTICS validation-tasks,new,load,providers]
// @BRIEF Load function for the new validation task page — fetches LLM providers list.
// @RELATION CALLS -> [ApiModule.getLlmProviders]
// @POST Returns { llmProviders }
import { api } from '$lib/api.js';
import { log } from '$lib/cot-logger';
import type { PageLoad } from './$types';

interface ProvidersResponse {
	providers?: unknown[];
	results?: unknown[];
	[key: string]: unknown;
}

interface LoadResult {
	llmProviders: unknown[];
	providersError?: string;
}

export const load: PageLoad = async (): Promise<LoadResult> => {
	try {
		const providers: unknown[] | ProvidersResponse = await api.getLlmProviders();
		return {
			llmProviders: Array.isArray(providers) ? providers : ((providers as ProvidersResponse)?.providers ?? (providers as ProvidersResponse)?.results ?? []),
		};
	} catch (e: unknown) {
		const message = e instanceof Error ? e.message : 'unknown';
		log('validation-tasks/new', 'EXPLORE', 'Failed to load providers', {}, message);
		return {
			llmProviders: [],
			providersError: message || 'Failed to load providers',
		};
	}
};
// #endregion ValidationTaskNewPageLoad
