import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { backendFetch } from '$lib/server/scraper';

/**
 * GET /api/announce?text=...&voice=...&format=...
 *
 * Thin proxy to backend GET /api/tts-announce.
 * No paywall — this is a short announcement clip (a few words), not chapter audio.
 * No MinIO caching — the backend streams the clip directly.
 */
export const GET: RequestHandler = async ({ url }) => {
	const text = url.searchParams.get('text') ?? '';
	if (!text) error(400, 'text is required');

	const qs = new URLSearchParams();
	qs.set('text', text);

	const voice = url.searchParams.get('voice');
	if (voice) qs.set('voice', voice);

	const format = url.searchParams.get('format') ?? 'mp3';
	qs.set('format', format);

	const backendRes = await backendFetch(`/api/tts-announce?${qs}`);

	if (!backendRes.ok) {
		error(backendRes.status as Parameters<typeof error>[0], 'TTS announce failed');
	}

	return new Response(backendRes.body, {
		status: 200,
		headers: {
			'Content-Type': backendRes.headers.get('Content-Type') ?? 'audio/mpeg',
			'Cache-Control': 'no-store',
			'X-Accel-Buffering': 'no'
		}
	});
};
