import React from 'react';
import { type Locale } from '@/i18n';
import CheckoutSuccessClient from '@/components/checkout/CheckoutSuccessClient';

interface SuccessPageProps {
  params: Promise<{ locale: Locale }>;
  searchParams: Promise<{ booking_id?: string; intent_id?: string; ref?: string; id?: string; checkout_id?: string }>;
}

export const metadata = {
  title: 'Payment Confirmed | ShineCode Dubai',
  robots: { index: false, follow: false },
};

export default async function CheckoutSuccessPage({ params, searchParams }: SuccessPageProps) {
  const { locale } = await params;
  const { booking_id, intent_id, ref, id, checkout_id } = await searchParams;

  const resolvedBookingId = booking_id || (id && isNumeric(id) ? id : undefined) || (ref && isNumeric(ref) ? ref : undefined);
  const resolvedIntentId = intent_id || (id && !isNumeric(id) ? id : undefined) || (ref && !isNumeric(ref) ? ref : undefined);

  return (
    <CheckoutSuccessClient
      locale={locale}
      initialBookingId={resolvedBookingId}
      intentId={resolvedIntentId}
      checkoutId={checkout_id}
    />
  );
}

function isNumeric(val: string): boolean {
  return /^\d+$/.test(val);
}
