import { redirect } from 'next/navigation'

type CoursePageProps = {
  params: Promise<{ slug: string[] }>,
}

export default async function CatchAllRoute({ params }: CoursePageProps) {
  const { slug } = await params

  // If URL starts with a number (e.g., /2612/project-management...), redirect to /courses/2612/slug/
  // The backend redirect view will handle the rest and redirect to the correct course slug
  if (slug && slug.length > 0 && /^[0-9]+$/.test(slug[0])) {
    const courseSlug = slug.slice(1).join('/')
    redirect(`/courses/${slug[0]}/${courseSlug}/`)
  }

  // For any other catch-all, redirect to home
  redirect('/')
}
