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,
  isMode,
  localizedModePath,
  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 { ThemePreference } from '@/lib/theme/config';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';

interface PendingRouteProps {
  params: Promise<{ locale: string; mode: 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, mode: modeValue, slug } = await params;
  if (!isLocale(localeValue)) notFound();
  if (!isMode(modeValue)) notFound();
  const routeId = routeIdFromSlug(localeValue, slug);
  if (!routeId || routeId === 'home') notFound();
  return { locale: localeValue as Locale, mode: modeValue as ThemePreference, routeId };
}

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

export default async function PendingRoute({ params }: PendingRouteProps) {
  const { locale, mode, 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={localizedModePath('home', locale, mode)}>
          {messages.states.backHome}
        </StateAction>
      </StateActions>
    </StateShell>
  );
}
