import {
  defaultMode,
  isLocale,
  localizedModePath,
  type Locale,
} from '@/lib/localization/config';
import { isThemePreference, themeCookie } from '@/lib/theme/config';
import { cookies } from 'next/headers';
import { notFound, redirect } from 'next/navigation';

interface LocaleRedirectPageProps {
  params: Promise<{ locale: string }>;
}

async function preferredMode() {
  const cookieStore = await cookies();
  const cookieMode = cookieStore.get(themeCookie)?.value;
  return isThemePreference(cookieMode) ? cookieMode : defaultMode;
}

export default async function LocaleRedirectPage({ params }: LocaleRedirectPageProps) {
  const { locale: localeValue } = await params;
  if (!isLocale(localeValue)) notFound();
  redirect(localizedModePath('home', localeValue as Locale, await preferredMode()));
}
