import type { Handle } from '@sveltejs/kit';

const API_BACKEND = process.env.VITE_API_URL || 'http://localhost:8087';
const IMMICH_URL = process.env.IMMICH_URL || '';
const IMMICH_API_KEY = process.env.IMMICH_API_KEY || '';

export const handle: Handle = async ({ event, resolve }) => {
	// Direct Immich thumbnail proxy (bypass Python backend for speed)
	if (event.url.pathname.startsWith('/api/immich/thumb/') && IMMICH_URL && IMMICH_API_KEY) {
		const assetId = event.url.pathname.split('/').pop();
		try {
			const response = await fetch(`${IMMICH_URL}/api/assets/${assetId}/thumbnail`, {
				headers: { 'x-api-key': IMMICH_API_KEY }
			});
			return new Response(response.body, {
				status: response.status,
				headers: {
					'Content-Type': response.headers.get('Content-Type') || 'image/webp',
					'Cache-Control': 'public, max-age=86400'
				}
			});
		} catch {
			return new Response('', { status: 502 });
		}
	}

	// Proxy /api/* and /images/* requests to the Python backend
	if (event.url.pathname.startsWith('/api/') || event.url.pathname.startsWith('/images/')) {
		const targetUrl = `${API_BACKEND}${event.url.pathname}${event.url.search}`;

		const headers = new Headers();
		// Forward relevant headers
		for (const [key, value] of event.request.headers.entries()) {
			if (['authorization', 'content-type', 'cookie', 'x-api-key'].includes(key.toLowerCase())) {
				headers.set(key, value);
			}
		}

		// For image/asset requests without auth (e.g. <img> tags), use the server-side API key
		if (!headers.has('authorization') && !headers.has('cookie')) {
			const apiKey = process.env.TRIPS_API_KEY;
			if (apiKey) {
				headers.set('Authorization', `Bearer ${apiKey}`);
			}
		}

		try {
			const response = await fetch(targetUrl, {
				method: event.request.method,
				headers,
				body: event.request.method !== 'GET' && event.request.method !== 'HEAD'
					? await event.request.arrayBuffer()
					: undefined,
			});

			return new Response(response.body, {
				status: response.status,
				headers: response.headers,
			});
		} catch (err) {
			console.error('Proxy error:', err);
			return new Response(JSON.stringify({ error: 'Backend unavailable' }), {
				status: 502,
				headers: { 'Content-Type': 'application/json' },
			});
		}
	}

	return resolve(event);
};
