import { notFound } from 'next/navigation';
import { cookies, headers } from 'next/headers';
import { resolveLocale, t } from '../../lib/i18n';

type Props = {
  searchParams: { token?: string };
};

export default async function VerifyPage({ searchParams }: Props) {
  const token = searchParams.token;
  if (!token) {
    notFound();
  }
  const locale = resolveLocale({ cookieLocale: cookies().get('locale')?.value, acceptLanguage: headers().get('accept-language') });
  const translate = (key: any) => t(locale, key as any);

  const res = await fetch(`${process.env.APP_URL ?? 'http://localhost:3000'}/api/auth/verify`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ token }),
    cache: 'no-store',
  });
  const ok = res.ok;

  return (
    <main className="panel" style={{ maxWidth: 520, margin: '40px auto' }}>
      <h1>{translate('verifyTitle')}</h1>
      {ok ? (
        <p>{translate('verifyOk')}</p>
      ) : (
        <p style={{ color: 'red' }}>{translate('verifyFail')}</p>
      )}
    </main>
  );
}
