import { StateAction, StateActions, StateShell } from '@/components/app-shell/StateShell';
import { SignInForm } from '@/components/auth/SignInForm';
import { ForgotPasswordForm } from '@/components/auth/ForgotPasswordForm';
import { ResetPasswordForm } from '@/components/auth/ResetPasswordForm';
import {
  isLocale,
  localizedPath,
  routeIdFromSlug,
  type Locale,
  type RouteId,
} from '@/lib/localization/config';
import { getMessages, type ShellMessages } from '@/lib/localization/messages';
import { buildLocalizedMetadata } from '@/lib/metadata/build-metadata';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';

interface PendingRouteProps {
  params: Promise<{ locale: string; slug: string }>;
}

function routeLabel(messages: ShellMessages, routeId: RouteId): string {
  const labels = messages.footer.links;
  switch (routeId) {
    case 'sign-in':
      return labels.login;
    case 'forgot-password':
      return 'Forgot password';
    case 'reset-password':
      return 'Reset password';
    case 'privacy':
      return labels.privacy;
    case 'terms':
      return labels.terms;
    case 'accessibility':
      return labels.accessibility;
    case 'home':
      return 'RentalDriveGo';
  }
}

async function resolveParams(params: PendingRouteProps['params']) {
  const { locale: localeValue, slug } = await params;
  if (!isLocale(localeValue)) notFound();
  const routeId = routeIdFromSlug(localeValue, slug);
  if (!routeId || routeId === 'home') notFound();
  return { locale: localeValue as Locale, routeId };
}

export async function generateMetadata({ params }: PendingRouteProps): Promise<Metadata> {
  const { locale, routeId } = await resolveParams(params);
  const messages = getMessages(locale).shell;
  const label = routeLabel(messages, routeId);
  return buildLocalizedMetadata({
    locale,
    routeId,
    title: `${label} | ${messages.metadata.pendingTitle}`,
    description: messages.metadata.pendingDescription,
    indexable: false,
  });
}

export default async function PendingRoute({ params }: PendingRouteProps) {
  const { locale, routeId } = await resolveParams(params);
  const messages = getMessages(locale).shell;
  const label = routeLabel(messages, routeId);

  if (routeId === 'sign-in') {
    return <SignInForm locale={locale} />;
  }

  if (routeId === 'forgot-password') {
    return <ForgotPasswordForm locale={locale} />;
  }

  if (routeId === 'reset-password') {
    return <ResetPasswordForm locale={locale} />;
  }

  return (
    <StateShell
      title={`${label}: ${messages.states.pendingTitle}`}
      body={messages.states.pendingBody}
    >
      <StateActions>
        <StateAction href={localizedPath('home', locale)}>{messages.states.backHome}</StateAction>
      </StateActions>
    </StateShell>
  );
}
