import { redirect } from 'next/navigation';

type RemixPageProps = {
  searchParams: Promise<{
    type?: 'cover' | 'extend';
    song_id?: string;
    style?: string;
    lyrics?: string;
    [key: string]: string | undefined;
  }>;
};

export default async function RemixPage({ searchParams }: RemixPageProps) {
  const params = await searchParams;
  const { type, song_id, ...otherParams } = params;

  // If missing required parameters, redirect to create page
  if (!song_id) {
    redirect('/create');
  }

  // Build the new URL parameters for the create page
  const urlParams = new URLSearchParams();
  if (type !== undefined) {
    urlParams.set('remix_type', type);
  }
  urlParams.set('song_id', song_id);

  // Add all other parameters as-is
  Object.entries(otherParams).forEach(([key, value]) => {
    if (value) {
      urlParams.set(key, value);
    }
  });

  // Redirect to create page with transformed parameters
  redirect(`/create?${urlParams.toString()}`);
}
