import type { APIRoute } from 'astro';
import { IS_DEV } from 'astro:env/server';

// Validated by the astro:env schema (astro.config.mjs): true or false, defaults to false.
const isDev = IS_DEV;

// Rendered at build time. The container image is built with IS_DEV=false, and
// docker/40-legyennyugta-env.sh replaces this file with the disallow-all version
// (no Sitemap line) at start when the environment sets IS_DEV=true (DEV, TST).
const getRobotsTxt = (sitemapURL: URL): string => {
	if (isDev) {
		return [
			'User-agent: *',
			'Disallow: /'
		].join('\n');
	}

	return [
		'User-agent: *',
		'Allow: /',
		'',
		`Sitemap: ${sitemapURL.href}`
	].join('\n');
};

export const GET: APIRoute = ({ site }) => {
	const sitemapURL = new URL('sitemap-index.xml', site);
	return new Response(getRobotsTxt(sitemapURL), {
		headers: { 'Content-Type': 'text/plain; charset=utf-8' },
	});
};
