/**
 * VAPID Public Key API
 *
 * Returns the public key for web push notifications.
 * Required by browsers to create push subscriptions.
 */

import { json } from '@sveltejs/kit';
import { pushNotificationService } from '$lib/server/notifications/PushNotificationService.js';
import type { RequestHandler } from './$types.js';

/**
 * Get VAPID public key
 *
 * GET /api/notifications/vapid-key
 *
 * Response:
 * {
 *   "publicKey": "BDummyPublicKeyForDevelopment",
 *   "applicationServerKey": "BDummyPublicKeyForDevelopment"
 * }
 */
export const GET: RequestHandler = async () => {
	try {
		const publicKey = pushNotificationService.getPublicVapidKey();

		if (!publicKey) {
			return json({ error: 'VAPID public key not configured' }, { status: 503 });
		}

		return json({
			publicKey,
			applicationServerKey: publicKey // Alias for compatibility
		});
	} catch (error) {
		console.error('[NotificationAPI] VAPID key error:', error);
		return json({ error: 'Failed to get VAPID public key' }, { status: 500 });
	}
};
