import { json } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { checkLLMHealth, isModelLoaded } from '$lib/server/llm';

/**
 * Health check endpoint for the LLM service (llama-swap on ideapad).
 *
 * Three states:
 *  - ok      → endpoint reachable AND configured model is loaded in VRAM
 *  - warming → endpoint reachable but configured model not yet loaded
 *              (next request will trigger a cold load)
 *  - error   → endpoint unreachable
 */
export async function GET() {
	try {
		const reachable = await checkLLMHealth();
		const configuredModel = env.LLM_MODEL || 'gpt-4o';

		if (!reachable) {
			return json(
				{
					status: 'error',
					message: 'LLM service is not accessible',
					configuredModel
				},
				{ status: 503 }
			);
		}

		const warm = await isModelLoaded(configuredModel);
		return json({
			status: warm ? 'ok' : 'warming',
			message: warm
				? `Model ${configuredModel} loaded and ready`
				: `Model ${configuredModel} configured; next request will trigger a cold load`,
			configuredModel,
			loaded: warm
		});
	} catch (error) {
		const errorMessage = error instanceof Error ? error.message : 'Unknown error';
		return json(
			{
				status: 'error',
				message: errorMessage,
				configuredModel: env.LLM_MODEL || 'gpt-4o'
			},
			{ status: 500 }
		);
	}
}
