import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getSurrealDB } from '$lib/server/db';

export const GET: RequestHandler = async () => {
	try {
		const db = await getSurrealDB();
		
		const result = await db.query<[any[]]>('SELECT * FROM country');
		console.log('[DEBUG] Raw query result:', result);
		
		const countries = result[0] || [];
		console.log('[DEBUG] Countries found:', countries.length);
		
		return json({
			success: true,
			count: countries.length,
			countries: countries.map(c => ({ id: c.id, name: c.name, code: c.code }))
		});
	} catch (error) {
		console.error('[DEBUG] Error:', error);
		return json({
			success: false,
			error: String(error)
		}, { status: 500 });
	}
};
