import { error, redirect } from '@sveltejs/kit';
import { staticPages, type StaticPageSlug } from '$lib/content/static-pages';
import { isGeneralEnquiryEnabled } from '$lib/server/feature-flags';
import { getSharedPageContent } from '$lib/server/content';

export async function load({ params }) {
  if (params.slug === 'about-us') {
    throw redirect(301, '/about');
  }

  if (params.slug === 'booking') {
    throw redirect(301, '/contact-us');
  }

  if (params.slug === 'home-stays') {
    throw redirect(301, '/');
  }

  const slug = params.slug as StaticPageSlug;
  const generalEnquiryEnabled = isGeneralEnquiryEnabled();
  const sourcePage = staticPages[slug];

  if (!sourcePage) {
    throw error(404, 'Page not found');
  }

  const page =
    slug === 'contact-us' && !generalEnquiryEnabled
      ? {
          ...sourcePage,
          description: 'Book a Meet & Greet with Goodwalk Auckland dog walking services.'
        }
      : sourcePage;

  return {
    content: await getSharedPageContent(),
    generalEnquiryEnabled,
    page,
    slug
  };
}
