import { goto } from '$app/navigation';
import { redirect } from '@sveltejs/kit';

/** @satisfies {import('./$types').Actions} */
export const actions = {
	create: async ({ request }) => {
		const formData = await request.formData();
		const title = formData.get('title');
		const content = formData.get('content');

		const response = await fetch('http://no08g0o48sc8w0woowwsskos.134.255.231.190.sslip.io/api/note', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({ title, content }),
		});

		if (!response.ok) {
			const error = await response.json();
			return { success: false, error: error.message || 'Failed to create note.' };
		}

		const data = await response.json();

		return goto("/")
	}
}